Factory Girl 对象图的公共根

发布于 2024-12-14 07:26:06 字数 678 浏览 1 评论 0原文

invitation ------>  event
  \                     \
  \/                    \/
 responder(person) ---->account
      \                  /\
       \                 /
       group-------------

我有上面的关联图。 以及下面的工厂代码:

Factory.define :invitation do |i|
  i.association :event
  i.association :responder, :factory => :person
end


Factory.define :event do |e|
  e.association :account
end


Factory.define :person do |p|
  p.association :account
  p.association :group
end

Factory.define :group do |g|
  g.association :account
end

如果我想使用“Factory :invitation”创建邀请,那么该帐户将被创建多次。并且第二次尝试已经占用了帐户的唯一字段。 有什么聪明又好的收据吗?

invitation ------>  event
  \                     \
  \/                    \/
 responder(person) ---->account
      \                  /\
       \                 /
       group-------------

I have the above association graph.
And the below factory code:

Factory.define :invitation do |i|
  i.association :event
  i.association :responder, :factory => :person
end


Factory.define :event do |e|
  e.association :account
end


Factory.define :person do |p|
  p.association :account
  p.association :group
end

Factory.define :group do |g|
  g.association :account
end

If I want to create an invitation with "Factory :invitation" then the account would be created more than once. And the unique fields for account are already taken by the second attempt.
Is there any clever and nice receipt for this?

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

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

发布评论

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

评论(1

简单 2024-12-21 07:26:06

您可以在 after_create (或 after_build)回调中重用事件帐户。

Factory.define :invitation do |i|
  i.association :event
  i.after_create { |i| i.responder = Factory(:person, :account => i.event.account) }
end

您必须以类似的方式修改组工厂。

You can reuse the account of event in the after_create (or after_build) callback.

Factory.define :invitation do |i|
  i.association :event
  i.after_create { |i| i.responder = Factory(:person, :account => i.event.account) }
end

You'll have to modify the group factory in a similar way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文