工厂女孩+ Mongoid 在装置中嵌入文档
假设您有以下 mongoid 文档:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :last_initial
embedded_in :user
end
如何创建一个工厂女孩工厂来初始化嵌入的名字和姓氏首字母?另外,您将如何利用 embeds_many
关系来做到这一点?
Let’s say you have the following mongoid documents:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :last_initial
embedded_in :user
end
How do you create a factory girl factory which initializes the embedded first name and last initial? Also how would you do it with an embeds_many
relationship?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也在寻找这个,当我研究时,我偶然发现了很多代码,并将它们全部拼凑在一起(尽管我希望有更好的文档),但这是我的代码部分。地址与事件是 1..1 关系,电话是 1..n 关系。
(抱歉,如果我无法提供链接,它们有点混乱)
I was also looking for this one and as I was researching I've stumbled on a lot of code and did pieced them all together (I wish there were better documents though) but here's my part of the code. Address is a 1..1 relationship and Phones is a 1..n relationship to events.
(And sorry if I can't provide my links, they were kinda messed up)
这是一个允许您动态定义嵌入对象数量的解决方案:
这允许您调用 FactoryGirl.create(:profile_with_notes) 并获取两个嵌入注释,或者调用 FactoryGirl.create( :profile_with_notes,notes_count: 5) 并获得五个嵌入式注释。
Here is a solution that allows you to dynamically define the number of embedded objects:
This allows you to call
FactoryGirl.create(:profile_with_notes)
and get two embedded notes, or callFactoryGirl.create(:profile_with_notes, notes_count: 5)
and get five embedded notes.