嵌入式 monoid 引用
我的User
模型has_and_belongs_to_many :conversations
。
Conversation
模型 embeds_many :messages
。
Message
模型需要有一个发送者
和一个接收者
。
我无法在 Mongoid 文档 中找到 referenced_in
。
如何分配消息中的用户?我尝试遵循类似于此实现,但不断收到 BSON::InvalidDocument: Cannot serialize an object of class Mongoid::Relations::Referenced::In into BSON。
2013 年 11 月更新:reference_in
不再适用于 Mongoid 3.0?更改为 belongs_to
,效果似乎相同。
My User
model has_and_belongs_to_many :conversations
.
The Conversation
model embeds_many :messages
.
The Message
model needs to have a sender
and a recipient
.
I was not able to find referenced_in
at the Mongoid documentation.
How do I assign the users in the message? I tried to follow something similar to this implementation, but kept getting BSON::InvalidDocument: Cannot serialize an object of class Mongoid::Relations::Referenced::In into BSON.
November 2013 Update: reference_in
no longer works with Mongoid 3.0? Changed to belongs_to
and it seems to work the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,引用
User
的Message
结构是合适的,并且序列化错误与将用户与对话相关联。这是我的结构和创建步骤。我感谢任何有关更好实践的反馈,谢谢。在我尝试
@conversation.build(user_ids: [@user_one,@user_two])
之前,适当的方法是@conversation.users.concat([@user_one,@user_two] ])
。然后你可以简单地@conversation.messages.build(sender: @user_one,recipient: @user_two)
。As it turns out, my structure of the
Message
referencing theUser
was appropriate, and the serialization error was related to associating the Users with the Conversation. Here is my structure and the creation steps. I appreciate any feedback on better practices, thanks.Where before I was trying to
@conversation.build(user_ids: [@user_one,@user_two])
, the appropriate way is to@conversation.users.concat([@user_one,@user_two])
. Then you can simply@conversation.messages.build(sender: @user_one, recipient: @user_two)
.