How to set up factory in FactoryGirl with has_many association

前端 未结 5 768
深忆病人
深忆病人 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:38

    Creating these kinds of associations requires using FactoryGirl's callbacks.

    A perfect set of examples can be found here.

    https://thoughtbot.com/blog/aint-no-calla-back-girl

    To bring it home to your example.

    Factory.define :listing_with_features, :parent => :listing do |listing|
      listing.after_create { |l| Factory(:feature, :listing => l)  }
      #or some for loop to generate X features
    end
    

提交回复
热议问题