2015-08-26 3 views
2

Я следую принятый ответ от сюда Factory Girl: How to setup a has_many/through associationзавод девушка has_many через ассоциацию

У меня есть has_many через ассоциацию

class Therapist < ActiveRecord::Base 

has_many :clients, dependent: :destroy 
has_many :courses, through: :clients 

На моем заводе код девушки

FactoryGirl.define do 
    factory :therapist do 
    sequence(:first_name) { |n| "John#{n}" } 
    sequence(:last_name) { |n| "Smith#{n}" } 

    factory :therapist_with_course do 
     after(:create) { |therapist| therapist.courses << FactoryGirl.create(:course) } 
    end 
    end 
end 

Курс фабрики

FactoryGirl.define do 
    factory :course do 
    client 
    end 
end 

Когда я запускаю тест я получаю следующую ошибку

Failure/Error: let(:therapist) { create :therapist_with_course } 
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: 
    Cannot modify association 'Therapist#courses' because the source reflection class 'Course' is associated to 'Client' via :has_many. 
+0

Как ассоциации, определенные в 'Course' и' под клиента моделей? – zetetic

+0

класс курс user3814030

ответ

3

Сначала therapist завод должен быть создан и после создания терапевта делается вы прилагая курс завод. В вашем случае вы factory :therapist_with_course не имеет никакой информации о therapist

FactoryGirl.define do 
    factory :therapist do 
     # The therapist attributes here 
     after(:create) { |therapist| therapist.courses << FactoryGirl.create(:course) } 
    end 
end 
Смежные вопросы