30

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

我有以下型号:

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 附加详细信息。

4

6 回答 6

33

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

factory :job do
  ...

  after_create do |job|
    job.details << FactoryGirl.create(:detail)
  end
end
于 2013-01-21T19:40:56.263 回答
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
于 2016-02-25T07:51:22.727 回答
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
于 2014-06-17T18:32:10.910 回答
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,它有选项来指定你想要多少细节。您可以阅读这篇有趣的文章了解更多详细信息。

于 2018-02-26T15:08:22.033 回答
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)
于 2021-03-19T14:22:32.513 回答
0

从 FactoryBot v5 开始,关联保留了构建策略。关联是解决这个问题的最好方法,文档中有很好的例子

FactoryBot.define :job do
  job_details { [association(:job_detail)] }
end

FactoryBot.define :detail do
  description "Full Time"
end

FactoryBot.define :job_detail do
  association :job
  association :detail
end
于 2021-12-14T20:53:59.633 回答