我一直在努力使用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
将Detail通过JobDetail附加。
DEPRECATION WARNING: You're trying to create an attribute
detail_id'. Writing arbitrary attributes on a model is deprecated. Please just useattr_writer
etc.` 你有什么想法吗? - cman77after(:create)
的回调函数,而不是after_create
。答案的其余部分应仍然可以正常工作,没有错误。 - Arelafter(:create)
回调的更多信息:http://robots.thoughtbot.com/get-your-callbacks-on-with-factory-girl-3-3 - Brianafter(:build)
呢?有没有办法为未保存的对象建立关联?还是我应该放弃以这种方式工作的想法? - Epigene