gpt4 book ai didi

rspec - 工厂女孩有_many通过协会

转载 作者:行者123 更新时间:2023-12-05 03:12:50 25 4
gpt4 key购买 nike

我正在关注这里接受的答案 Factory Girl: How to setup a has_many/through association

我有一个 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

类(class)工厂

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.

最佳答案

首先应该创建 therapist 工厂,在创建 therapist 完成后,您将追加类(class)工厂。在你的情况下,你 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

关于rspec - 工厂女孩有_many通过协会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com