FactoryGirl.define do
factory :agency do
name "Example Inc"
available_items "20"
recruiter # recruiter.id
end
factory :recruiter do
email '[email protected]'
password 'please'
password_confirmation 'please'
# required if the Devise Confirmable module is used
# confirmed_at Time.now
end
end
agency.rbFactory_girl兩款車型之間的簡單關聯
class Agency < ActiveRecord::Base
belongs_to :recruiter
validates :name, :presence => true
end
recruiter.rb
class Recruiter < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :agency_attributes, :first_name
has_one :agency, :dependent => :destroy
accepts_nested_attributes_for :agency
validates :email, :presence => true
end
authentication_steps.rb
def create_user
@recruiter = FactoryGirl.create(:recruiter)
end
如何使用factory_girl複製此招聘人員&機構關聯?
如果代理機構'belongs_to:招聘人員',那麼您只能爲每個代理機構申請一個招聘人員,並且您的代理機構表中有一個招聘人員ID。那是你要的嗎?我期望看到機構'has_many:招聘人員'和招聘人'belongs_to:agency'。 – 2012-04-24 17:35:43