加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 21:28:18 所属栏目:百科 来源:网络整理
导读:我需要创建一些由多个工厂组成的工厂 这是我的模特 Topic has_many :plan_topics has_many :plans,:through = :plan_topicsPlanTopic belongs_to :plan belongs_to :topicPlan has_many :subscriptions has_many :members,:through = :subscriptions has_man
我需要创建一些由多个工厂组成的工厂

这是我的模特

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

我想做的是 – 工厂(: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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读