簡體   English   中英

Factory Girl:如何建立has_many/through關聯

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

我一直在努力使用 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 附加 Detail。

嘗試這樣的事情。 您想要構建一個detail對象並將其附加到作業的詳細信息關聯。 當您使用after_create ,創建的作業將被交給區塊。 因此,您可以使用 FactoryGirl 創建詳細信息對象,並將其直接添加到該作業的詳細信息中。

factory :job do
  ...

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

我今天遇到了這個問題,我找到了解決方案。 希望這可以幫助某人。

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

這對我有用

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

您可以通過以下方式解決此問題:

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,它有選項來指定你想要的細節數量。 您可以閱讀這篇有趣的文章以了解更多詳細信息。

使用當前的 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)

從 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM