嵌入式 monoid 引用

发布于 2025-01-05 02:15:32 字数 773 浏览 0 评论 0原文

我的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 技术交流群。

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

发布评论

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

评论(1

双手揣兜 2025-01-12 02:15:32

事实证明,引用 UserMessage 结构是合适的,并且序列化错误与将用户与对话相关联。这是我的结构和创建步骤。我感谢任何有关更好实践的反馈,谢谢。

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  has_and_belongs_to_many :conversations
end
class Conversation
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  has_and_belongs_to_many :users
  embeds_many :messages
end
class Message
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  embedded_in :conversation
  embeds_one :sender, class_name: 'User'
  embeds_one :recipient, class_name: 'User'

  field :content
  field :read_at, type: DateTime
  field :sender_deleted, type: Boolean, default: false
  field :recipient_deleted, type: Boolean, default: false

  belongs_to :sender, class_name: "User", inverse_of: :sender, foreign_key: 'sender_id'
  belongs_to :recipient, class_name: "User", inverse_of: :recipient, foreign_key: 'recipient_id'
end

在我尝试 @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 the User 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.

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  has_and_belongs_to_many :conversations
end
class Conversation
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  has_and_belongs_to_many :users
  embeds_many :messages
end
class Message
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  embedded_in :conversation
  embeds_one :sender, class_name: 'User'
  embeds_one :recipient, class_name: 'User'

  field :content
  field :read_at, type: DateTime
  field :sender_deleted, type: Boolean, default: false
  field :recipient_deleted, type: Boolean, default: false

  belongs_to :sender, class_name: "User", inverse_of: :sender, foreign_key: 'sender_id'
  belongs_to :recipient, class_name: "User", inverse_of: :recipient, foreign_key: 'recipient_id'
end

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).

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