Ruby on rails 工厂女工倍数通过';s
我需要创建一些工厂,这些工厂是由多个具有多个直通的组件组成的 这是我的模型Ruby on rails 工厂女工倍数通过';s,ruby-on-rails,factory-bot,Ruby On Rails,Factory Bot,我需要创建一些工厂,这些工厂是由多个具有多个直通的组件组成的 这是我的模型 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
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.define :member_with_subscription, :class => 'Member' do |m|
m.after_build do |member|
member.subscriptions << Factory.build(:subscription)
end
end
Factory.define:member_与_订阅,:class=>'member'do|m|
m、 建造完成后|成员|
member.subscriptions我的做法略有不同,这种方式可能更容易理解:
FactoryGirl.define do
factory :member_with_description do
after(:build) do |member|
member.subscriptions << FactoryGirl.build(:subscription)
end
end
end
FactoryGirl.define do
工厂:带有描述的成员
后(:build)do |成员|
会员订阅
FactoryGirl.define do
factory :member_with_description do
after(:build) do |member|
member.subscriptions << FactoryGirl.build(:subscription)
end
end
end