30

has_many/throughFactory 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_createJobDetail を介して Detail をアタッチするために をどのように使用すべきかわかりません。

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 に答える