How to set up factory in FactoryGirl with has_many association

前端 未结 5 770
深忆病人
深忆病人 2020-12-07 12:54

Can someone tell me if I\'m just going about the setup the wrong way?

I have the following models that have has_many.through associations:

class List         


        
5条回答
  •  感动是毒
    2020-12-07 13:47

    Alternatively, you can use a block and skip the association keyword. This makes it possible to build objects without saving to the database (otherwise, a has_many association will save your records to the db, even if you use the build function instead of create).

    FactoryGirl.define do
      factory :listing_with_features, :parent => :listing do |listing|
        features { build_list :feature, 3 }
      end
    end
    

提交回复
热议问题