ファクトリーガールを使用してhas_many/through
の関係を設定することに苦労しています。工場の女の子:has_many/throughアソシエーションを設定する方法
私は次のモデルがあります:
class Job < ActiveRecord::Base
has_many :job_details, :dependent => :destroy
has_many :details, :through => :job_details
end
class Detail < ActiveRecord::Base
has_many :job_details, :dependent => :destroy
has_many :jobs, :through => :job_details
end
class JobDetail < ActiveRecord::Base
attr_accessible :job_id, :detail_id
belongs_to :job
belongs_to :detail
end
私の工場:私が欲しいもの
factory :job do
association :tenant
title { Faker::Company.catch_phrase }
company { Faker::Company.name }
company_url { Faker::Internet.domain_name }
purchaser_email { Faker::Internet.email }
description { Faker::Lorem.paragraphs(3) }
how_to_apply { Faker::Lorem.sentence }
location "New York, NY"
end
factory :detail do
association :detail_type <--another Factory not show here
description "Full Time"
end
factory :job_detail do
association :job
association :detail
end
は「フルタイム」のデフォルトDetail
で作成することが私の仕事の工場のためです。
私はこれに従うことをしようとしてきたが、運がなかった。 FactoryGirl Has Many through
私はafter_create
がJobDetail経由で詳細を取り付けるために使用する必要があるかどうかはわかりません。
ありがとうございました。 1つの質問 - after_createの動作を追加しますが、 'DEPRECATION WARNING:' detail_id '属性を作成しようとしています。モデルに任意の属性を書き込むことは推奨されていません。 'attr_writer'などを使ってください。 – cman77
私はこれが古いと知っていますが、FactoryGirlでは 'after_create'の代わりに' after(:create) 'という形式のコールバックを使用します。残りの答えはエラーなく動作するはずです。 – Arel
'after(:create)'コールバックの詳細:http://robots.thoughtbot.com/get-your-callbacks-on-with-factory-girl-3-3 – Brian