FactoryGirl で非常に単純な関連付けを設定しましたが、何らかの理由で機能しません。このアプリはツアー会社向けであるため、各ツアーには目的地があります (私は ActiveRecord を使用しているため、Tour
モデルbelongs_to
a Destination
-- この関連付けはすべてのツアーに必要です)。そこで、工場を次のように設定しました。
# spec/factories.rb
FactoryGirl.define do
factory :destination do
name "Example Destination"
city "Example City"
state "CA"
end
factory :tour do
departure_date { 1.day.from_now }
return_date { 1.day.from_now }
destination
end
end
ただし、 を呼び出すとFactoryGirl.attributes_for(:tour)
、次のように返されます。
{:departure_date => Mon, 04 Nov 2013 17:51:26 UTC +00:00,
:return_date => Mon, 04 Nov 2013 17:51:26 UTC +00:00}
いいえdestination_id
!なんで?
destination
工場は問題なく動いています。
関連付けを古い方法で定義しようとしましassociation :destination, factory: :destination
たが、それは役に立ちませんでした。destination_id
asを手動で定義すれば動作させることができますが、destination_id FactoryGirl.create(:destination).id
そうする必要はありません。なぜそれが機能しないのですか?