Appointment
모델의 FactoryGirl 팩토리가 있습니다. 예 :AR 콜백의 논리로 인해 FactoryGirl 팩토리가 실패합니다.
require 'faker'
FactoryGirl.define do
factory :appointment do |f|
f.name { 'Pending Appointment' }
end
end
Appoinment
모델은 많은 AppointmentAccess
경우가 있습니다. Appointment
모델의 ActiveRecord 콜백에 생성합니다. AppointmentAccess
은 Appointment
을 User
과 관련된 통과 모델입니다.
Appointment
모델의 AR 콜백에서 발생되는 다음부터
class Appointment < ActiveRecord::Base
has_many :appointment_accesses
has_many :users, through: :appointment_accesses
after_create :example_callback
protected
def example_callback
owner = self.appointment_accesses.find_by(owner: 1)
owner.name
end
end
모델의 콜백이 FactoryGirl 콜백보다 먼저 실행되고 있는데 AppointmentAccess
이 owner
으로 설정된 모델이 아직 존재하지 않기 때문에 오류가 발생합니다. 다음은 콜백 내 FactoryGirl 공장 (위와 동일 콜백으로)입니다 :
require 'faker'
FactoryGirl.define do
factory :appointment do |f|
f.name { 'Pending Appointment' }
after(:create) do |appointment|
FactoryGirl.create(:appointment_access, appointment: appointment)
end
end
end
나는 액티브 논리가 필요하기 때문에 FactoryGirl 콜백 (처음 실행되는지는 Appointment
모델에 콜백하기 전에 실행해야 할 수있는 방법 그것)?
어쩌면 당신의 문제에 도움이되지 않지만 콜백을 피하려고합니다. – Sebastian