2
FactoryGirl協會,我有以下的關聯用不同的名字
class Training < ApplicationRecord
has_many :attendances
has_many :attendees, through: :attendances
end
class Attendance < ApplicationRecord
belongs_to :training
belongs_to :attendee, class_name: 'Employee'
考勤表有attendee_id
和training_id
。
現在,我如何使用FactoryGirl創建有效的Attendance
?
目前,我有以下的代碼
FactoryGirl.define do
factory :attendance do
training
attendee
end
end
FactoryGirl.define do
factory :employee, aliases: [:attendee] do
sequence(:full_name) { |n| "John Doe#{n}" }
department
end
end
,但我得到
NoMethodError:
undefined method `employee=' for #<Attendance:0x007f83b163b8e8>
我也曾嘗試
FactoryGirl.define do
factory :attendance do
training
association :attendee, factory: :employee
end
end
有了相同的結果。
感謝您的幫助(或有禮貌是不允許的SO ???)。