gpt4 book ai didi

ruby-on-rails - 工厂女孩多个has_many通过的

转载 作者:行者123 更新时间:2023-12-04 13:20:43 25 4
gpt4 key购买 nike

我需要创建一些工厂,这些工厂由多个具有多个

这是我的模特

Topic
has_many :plan_topics
has_many :plans, :through => :plan_topics

PlanTopic
belongs_to :plan
belongs_to :topic

Plan
has_many :subscriptions
has_many :members, :through => :subscriptions
has_many :plan_topics
has_many :topics, :through => :plan_topics

Subscription
belongs_to :member
belongs_to :plan

Member
has_many :subscriptions
has_many :plans, :through => :subscriptions

这是我所拥有的
Factory.define :topic do |topic|
topic.name "Operations"
end

Factory.define :plan do |plan|
plan.title "A test Finance plan"
plan.price "200"
end

Factory.define :plan_topic do |plan_topic|
plan_topic.topic {|topic| topic.association(:topic)}
plan_topic.plan {|plan| plan.association(:plan)}
end

我想做的是-Factory(:member_with_subscription)
Factory.define :member_with_subscription do |subscription|
subscription.association(:plan_with_topic)
subscription.association(:member)
end

有没有办法做到这一点?

最佳答案

考虑使用after_build回调来设置所有必需的依赖项。例如:

Factory.define :member_with_subscription, :class => 'Member' do |m|
m.after_build do |member|
member.subscriptions << Factory.build(:subscription)
end
end

关于ruby-on-rails - 工厂女孩多个has_many通过的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8259074/

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