ruby-on-rails – FactoryGirl:填充一个有很多关系保存构建策略
发布时间:2020-12-16 19:34:49 所属栏目:百科 来源:网络整理
导读:我的问题似乎很常见,但我在文档或互联网本身没有找到任何答案. 它可能看起来像这个问题has_many while respecting build strategy in factory_girl的克隆,但是在那篇post factory_girl改变了很多之后的2.5年. 我有一个名为照片的has_many关系的模型.我想填充
我的问题似乎很常见,但我在文档或互联网本身没有找到任何答案.
它可能看起来像这个问题has_many while respecting build strategy in factory_girl的克隆,但是在那篇post factory_girl改变了很多之后的2.5年. 我有一个名为照片的has_many关系的模型.我想填充这个有很多关系,保留我选择的构建策略. 如果我打电话提供= FactoryGirl.build_stubbed:offer,:我希望offering.photos是一个存根模型的集合. 我发现实现这一目标的唯一方法是: factory :offering do association :partner,factory: :named_partner association :destination,factory: :geolocated_destination trait :stay do title "Hotel Gran Vía" description "Great hotel in a great zone with great views" offering_type 'stay' price 65 rooms 70 stars 4 event_spaces 3 photos do case @build_strategy when FactoryGirl::Strategy::Create then [FactoryGirl.create(:hotel_photo)] when FactoryGirl::Strategy::Build then [FactoryGirl.build(:hotel_photo)] when FactoryGirl::Strategy::Stub then [FactoryGirl.build_stubbed(:hotel_photo)] end end end end 没必要说IT必须存在更好的方法. 想法? 解决方法
这是一个稍微清洁的Flipstone版本的答案:
factory :offering do trait :stay do ... photos do association :hotel_photo,:strategy => @build_strategy.class end end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |