2 番目のモデルに関連付けられたモデルのファクトリがあり、それ自体が 3 番目のモデルに関連付けられている場合、ビルド戦略を保持することは可能ですか?
以下の例では、Post が User に関連付けられ、User が City に関連付けられています。:strategy => :build
すべての関連付けに使用されpost.user
、post.user.city
最終的にデータベースに保存される場合でも。迅速なテスト スイートのために、これらのデータベースへの書き込みが発生しないようにすることはできますか?
Factory.define do
factory :user do
name "A User"
association :city, :strategy => :build
end
factory :city do
name "A City"
end
factory :post do
title "A Post"
body "Some text here"
association :user, :strategy => :build
end
end
post = FactoryGirl.build(:post)
post.new_record? # True
post.user.new_record? # False
post.user.city.new_record? # False