2016-04-30 9 views
2

FactoryGirlとの多形関連のファクトリを設定する際に問題があります。Rails 4.2.6多相関連を持つFactoryGirl

class Address < ActiveRecord::Base 
    belongs_to :addressable, polymorphic: true 
end 

class Customer < ActiveRecord::Base 
    has_one :address, as: :addressable 
end 

factory :address do 
     village Faker::Address.city 
     upazilla Faker::Address.street_name 
     ward Faker::Address.street_address 
     district Faker::Address.state 
     association :addressable 
end 


factory :customer ,class: Customer do 
     recharge_token 10 
     date_of_birth Faker::Date.backward(100) 
     manager 
     after(:create) do |customer| 
     customer.address = create(:address, addressable: customer) 
     #create(:address, addressable: customer) 
     end  
end 

次のエラーメッセージ

vendor/cache/ruby/2.2.0/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:12:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError) 
* address - Factory not registered: addressable (ArgumentError) 

Thisとテストスイートの区切りが正確に私の問題ですが、残念ながら彼のソリューションは、私のために働いていない:私のモデルと工場出荷時の設定は次のようになります。あなたのすべての時間をありがとう!

答えて

8

明示的に工場を指定します。

factory :address do 
    association :addressable, factory: :customer 
end 

またはafter createリスト内の他のモデルに関連has_one作成:

factory :customer do 
    after(:create) do |customer| 
    customer.address = create(:address, addressable: customer) 
    end 
end 
+0

をねえ、あなたのトップソリューションが素晴らしいです。これを配列にすることはできますか? factory:[:customer、:user、:client] –

関連する問題