工厂女孩多个has_many通过的 [英] factory girl multiple has_many through's
本文介绍了工厂女孩多个has_many通过的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一些工厂,这些工厂由多个具有很多贯通的工厂组成
I need to create some factories that are made of multiple has many through's
这是我的模特
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)
What I would like to do is this - Factory(:member_with_subscription)
Factory.define :member_with_subscription do |subscription|
subscription.association(:plan_with_topic)
subscription.association(:member)
end
有没有办法做到这一点?
Is there a way of doing this ?
推荐答案
考虑使用after_build
回调设置所有必需的依赖项.例如:
Consider using after_build
callback to set all required dependencies. For example:
Factory.define :member_with_subscription, :class => 'Member' do |m|
m.after_build do |member|
member.subscriptions << Factory.build(:subscription)
end
end
这篇关于工厂女孩多个has_many通过的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文