工厂女孩多重 has_many throughs

发布于 2024-12-17 18:09:38 字数 1074 浏览 1 评论 0 原文

我需要创建一些由多个组成的工厂

这是我的模型

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

有没有办法做到这一点?

I need to create some factories that are made of multiple has many through's

Here are my models

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

Here is what I have

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2024-12-24 18:09:38

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

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

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
原谅我要高飞 2024-12-24 18:09:38

我的做法略有不同,这种方式可能更容易理解:

FactoryGirl.define do
  factory :member_with_description do
    after(:build) do |member|
      member.subscriptions << FactoryGirl.build(:subscription)
    end
  end 
end

I do it slightly differently, and this way might be slightly easier to understand:

FactoryGirl.define do
  factory :member_with_description do
    after(:build) do |member|
      member.subscriptions << FactoryGirl.build(:subscription)
    end
  end 
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文