Factory Girl: How to set up a has_many/through association

后端 未结 4 1111
误落风尘
误落风尘 2020-12-30 20:57

I\'ve been struggling with setting up a has_many/through relationship using Factory Girl.

I have the following models:

class Job < Ac         


        
4条回答
  •  我在风中等你
    2020-12-30 21:32

    Try something like this. You want to build a detail object and append it to the job's detail association. When you use after_create, the created job will be yielded to the block. So you can use FactoryGirl to create a detail object, and add it to that job's details directly.

    factory :job do
      ...
    
      after_create do |job|
        job.details << FactoryGirl.create(:detail)
      end
    end
    

提交回复
热议问题