Embedding message replies inside message parent with mongodb using mongoid

丶灬走出姿态 提交于 2019-12-06 23:46:38
Sergio Tulentsev

Whether it is best practice or not is a highly debatable topic. For instance, you have to mind the object size limit (currently 4 MB, but will go up soon).

as for your question: I suggest that you change

  embeds_many :replies, :class_name => 'Message'
  embedded_in :message, :inverse_of => :replies

to

  embeds_many :replies, :class_name => 'Message'
  referenced_in :message

And you will also have to specify both connections manually (that should not be a problem, as they are probably immutable anyway).

irb(main):002:0> msg1 = Message.new :subject => 'new question'
=> #<Message _id: 4cc7699f457601d7e8000001, created_at: nil, body: nil, updated_at: nil, subject: "new question", read_at: nil, sender_deleted: false, message_id: nil, recipient_deleted: false>
irb(main):003:0> msg2 = Message.new :subject => 'first comment'
=> #<Message _id: 4cc769b6457601d7e8000002, created_at: nil, body: nil, updated_at: nil, subject: "first comment", read_at: nil, sender_deleted: false, message_id: nil, recipient_deleted: false>
irb(main):005:0> msg2.message = msg1
=> #<Message _id: 4cc7699f457601d7e8000001, created_at: nil, body: nil, updated_at: nil, subject: "new question", read_at: nil, sender_deleted: false, message_id: nil, recipient_deleted: false>
irb(main):007:0> msg1.replies << msg2
=> [#<Message _id: 4cc769b6457601d7e8000002, created_at: nil, body: nil, updated_at: nil, subject: "first comment", read_at: nil, sender_deleted: false, message_id: BSON::ObjectId('4cc7699f457601d7e8000001'), recipient_deleted: false>]
irb(main):008:0> msg1.save
=> true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!