0

私は非常に単純なポリアソシエーション設定をしています。私はトークンがプロバイダーやショップだけに存在することを検証しようとしています。しかし、私のリファクタが工場をボークしています。多形関連と受け入れられたネストされた属性を持つfactorygirlは、妥当性検査に失敗します

問題

あなたはトークンを持つショップすなわちFactoryGirl.createを作成した場合(:お店、:with_authentication_token)店が作成され、FGのように保存され得ることができないので、それが吹くまでは、それを処理しようとしていますか?誰かが店の工場をセットアップする正しい方向に私を指すことができますか?それは親であるので

ActiveRecord::RecordInvalid: Validation failed: Owner can't be blank

エラーは今のところ、プロバイダの工場が動作します。

PRYで作業していますか?

shop = FactoryGirl.build(:shop) 
shop.authentication_token_attributes = { token: 'test', owner: shop } 
shop.save 

create_table "authentication_tokens", force: :cascade do |t| 
    t.string "token",  limit: 255, null: false 
    t.datetime "created_at",    null: false 
    t.datetime "updated_at",    null: false 
    t.integer "owner_id", limit: 4, null: false 
    t.string "owner_type", limit: 255, null: false 
    end 

工場

FactoryGirl.define do 
    factory :shop do 
    provider 
    ... 

    trait :with_authentication_token do 
     before(:create) do |shop| 
     create(:authentication_token, owner: shop) 
     end 
     after(:build) do |shop| 
     build(:authentication_token, owner: shop) 
     end 
    end 

    trait :inactive do 
     active { false } 
    end 
    end 
end 

モデル

class Shop < ActiveRecord::Base 
    belongs_to :provider 
    has_one :authentication_token, as: :owner, dependent: :destroy 

    accepts_nested_attributes_for(:authentication_token, update_only: true) 

    ... 

    validates :authentication_token, presence: true, if: :shop_is_owner? 

    ... 

    private 

    def shop_is_owner? 
    return false if provider.authentication_token 
    true 
    end 
end 

class Provider < ActiveRecord::Base 
    ... 

    has_many :shops 
    has_one :authentication_token, as: :owner, dependent: :destroy 

    ... 

    accepts_nested_attributes_for(:authentication_token, update_only: true) 

end 


class AuthenticationToken < ActiveRecord::Base 
    belongs_to :owner, polymorphic: true 

    validates :token, 
      length: { maximum: 245 }, 
      presence: true, 
      uniqueness: true 

    validates :owner, presence: true 

    validate :unique_auth_token 

    def shop 
    return owner if owner_type == 'Shop' 
    end 

    def provider 
    return owner if owner_type == 'Provider' 
    end 

    private 

    def unique_auth_token 
    errors.add(:base, I18n.t('activerecord.errors.models.shop.no_auth_token_sharing')) if shop && shop.provider.authentication_token 
    end 
end 

答えて

3

ので、あなたは私があなた@chrishough

+0

を動作するはずだと思う

trait :with_authentication_token do before(:create) do |shop| shop.authentication_token = build(:authentication_token, owner: shop) end after(:build) do |shop| shop.authentication_token = build(:authentication_token, owner: shop) end end 

関連モデルがインスタンス化される前に、いずれかのモデルに保存し、関連

trait :with_authentication_token do before(:create) do |shop| create(:authentication_token, owner: shop) end after(:build) do |shop| build(:authentication_token, owner: shop) end end 
変更をトリガすることはできませんもしそれが助けても答えをupvoteする必要があります。:) –

+1

@ArupRakshitよろしくお願いします –

関連する問題