Rspec 工厂女工通过协会有很多经验

Rspec 工厂女工通过协会有很多经验,rspec,factory-bot,Rspec,Factory Bot,我在这里遵循公认的答案 我有很多朋友通过协会 class Therapist < ActiveRecord::Base has_many :clients, dependent: :destroy has_many :courses, through: :clients 当我运行测试时,我得到以下错误 Failure/Error: let(:therapist) { create :therapist_with_course } ActiveRecord::HasManyThrou

我在这里遵循公认的答案

我有很多朋友通过协会

class Therapist < ActiveRecord::Base

has_many :clients, dependent: :destroy
has_many :courses, through: :clients
当我运行测试时,我得到以下错误

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.
首先应该创建治疗师工厂,在创建治疗师之后,您将附加课程工厂。在你的案例中,你的工厂:治疗师和你的课程没有关于治疗师的信息


课程和客户端模型中的关联是如何定义的?类课程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.
FactoryGirl.define do
   factory :therapist do
      # The therapist attributes here
      after(:create) { |therapist| therapist.courses << FactoryGirl.create(:course) }
   end
end