Factory Girl:如何设置has_many / through关联

时间:2013-01-21 18:23:47

标签: ruby-on-rails bdd factory-bot

我一直在努力与Factory Girl建立has_many/through关系。

我有以下型号:

class Job < ActiveRecord::Base
  has_many :job_details, :dependent => :destroy
  has_many :details, :through => :job_details
end

class Detail < ActiveRecord::Base
  has_many :job_details, :dependent => :destroy
  has_many :jobs, :through => :job_details
end

class JobDetail < ActiveRecord::Base
  attr_accessible :job_id, :detail_id
  belongs_to :job
  belongs_to :detail
end

我的工厂:

factory :job do
  association     :tenant
  title           { Faker::Company.catch_phrase }
  company         { Faker::Company.name }
  company_url     { Faker::Internet.domain_name }
  purchaser_email { Faker::Internet.email }
  description     { Faker::Lorem.paragraphs(3) }
  how_to_apply    { Faker::Lorem.sentence }
  location        "New York, NY"
end

factory :detail do
  association :detail_type <--another Factory not show here
  description "Full Time"
end

factory :job_detail do
  association :job
  association :detail
end

我想要的是使用默认Detail“全职”创建我的作业工厂。

我一直在努力追随这一点,但没有任何运气: FactoryGirl Has Many through

我不确定应该如何使用after_create通过JobDetail附加详细信息。

5 个答案:

答案 0 :(得分:32)

尝试这样的事情。您想构建一个detail对象并将其附加到作业的详细关联。使用after_create时,创建的作业将生成块。因此,您可以使用FactoryGirl创建详细信息对象,并将其直接添加到该作业的详细信息中。

factory :job do
  ...

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

答案 1 :(得分:4)

我今天遇到了这个问题,并找到了解决方案。希望这有助于某人。

FactoryGirl.define do
  factory :job do

    transient do
      details_count 5 # if details count is not given while creating job, 5 is taken as default count
    end

    factory :job_with_details do
      after(:create) do |job, evaluator|
        (0...evaluator.details_count).each do |i|
          job.details << FactoryGirl.create(:detail)
        end
      end
    end
  end  
end

这允许创建像这样的工作

create(:job_with_details) #job created with 5 detail objects
create(:job_with_details, details_count: 3) # job created with 3 detail objects

答案 2 :(得分:2)

这对我有用

FactoryGirl.define do
  factory :job do

    # ... Do whatever with the job attributes here

    factory :job_with_detail do

      # In later (as of this writing, unreleased) versions of FactoryGirl
      # you will need to use `transitive` instead of `ignore` here
      ignore do
        detail { create :detail }
      end

      after :create do |job, evaluator|
        job.details << evaluator.detail
        job.save
        job_detail = job.job_details.where(detail:evaluator.detail).first

        # ... do anything with the JobDetail here

        job_detail.save
      end
    end
  end
end

之后

# A Detail object is created automatically and associated with the new Job.
FactoryGirl.create :job_with_detail

# To supply a detail object to be associated with the new Job.
FactoryGirl.create :job_with_detail detail:@detail

答案 3 :(得分:0)

您可以通过以下方式解决此问题:

FactoryBot.define do
  factory :job do
    # job attributes

    factory :job_with_details do
      transient do
        details_count 10 # default number
      end

      after(:create) do |job, evaluator|
        create_list(:details, evaluator.details_count, job: job)
      end
    end
  end
end

使用此功能,您可以创建一个job_with_details,其中包含指定所需详细信息的选项。 您可以阅读this interesting article了解更多详情。

答案 4 :(得分:0)

使用当前的 factory_bot(以前的 factory_girl)实现,一切都由 gem 处理,您无需创建然后将记录推送到 jobs.details 中。你只需要这个

factory :job do
  ...

  factory :job_with_details do
    transient do
      details_count { 5 }
    end

    after(:create) do |job, evaluator|
      create_list(:detail, evaluator.details_count, jobs: [job])
      job.reload
    end
  end  
end

下面的代码将生成 5 个详细作业

 create(:job_with_details)