【发布时间】:2011-09-06 08:45:18
【问题描述】:
这是一个处理嵌套文档、引用和嵌入文档的棘手问题。 您可以找到一个 zip 文件,其中包含说明该问题的示例项目。
我正在使用 导轨 v3.0.9 蒙哥 v1.8.3 Mongoid v2.2.0
楷模- 对话
- 留言
- 文字
- 媒体
链接
对话 embeds_many 消息(自动保存 => true)
- 消息 has_many Media (autosave => true)
- 文本继承自消息 (STI)
- 链接继承自链接 (STI)
- 对话 accept_nested_attributes_for :messages
- 一条消息 accept_nested_attributes_for :medias
在 Rails 控制台中,让我们做一些测试:
ruby-1.9.2-p180 :001 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
=> #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: nil, updated_at: nil, title: nil>
ruby-1.9.2-p180 :002 > c.messages
=> [#<Message _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">]
ruby-1.9.2-p180 :003 > c.messages.first.medias
=> [#<Media _id: 4e65c7275d156d0129000003, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c7275d156d0129000002'), url: "google.com">]
ruby-1.9.2-p180 :004 > c.save
=> true
ruby-1.9.2-p180 :005 > c.reload
=> #<Conversation _id: 4e65c7275d156d0129000001, _type: nil, created_at: 2011-09-06 07:09:33 UTC, updated_at: 2011-09-06 07:09:33 UTC, title: nil>
ruby-1.9.2-p180 :006 > c.messages
=> [#<Text _id: 4e65c7275d156d0129000002, _type: "Text", created_at: nil, updated_at: nil, content: "content">]
ruby-1.9.2-p180 :007 > c.messages.first.medias
=> [] #Should not be empty
现在如果你改变 Conversation Message 之间的关系如下:
- 对话有_许多消息
完美运行。 也有一些 Rails 控制台测试:
ruby-1.9.2-p180 :002 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url => 'google.com'}]}]})
=> #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: nil, updated_at: nil, title: nil>
ruby-1.9.2-p180 :003 > c.messages
=> [#<Message _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: nil, updated_at: nil, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">]
ruby-1.9.2-p180 :004 > c.messages.first.medias
=> [#<Media _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: nil, updated_at: nil, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">]
ruby-1.9.2-p180 :005 > c.save
=> true
ruby-1.9.2-p180 :006 > c.reload
=> #<Conversation _id: 4e65c6bb5d156d011a000004, _type: nil, created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, title: nil>
ruby-1.9.2-p180 :007 > c.messages
=> [#<Text _id: 4e65c6bb5d156d011a000005, _type: "Text", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, conversation_id: BSON::ObjectId('4e65c6bb5d156d011a000004'), content: "content">]
ruby-1.9.2-p180 :008 > c.messages.first.medias
=> [#<Link _id: 4e65c6bb5d156d011a000006, _type: "Link", created_at: 2011-09-06 07:07:54 UTC, updated_at: 2011-09-06 07:07:54 UTC, message_id: BSON::ObjectId('4e65c6bb5d156d011a000005'), url: "google.com">]
这里是示例 zip 文件的链接:http://d.pr/oMUc
【问题讨论】:
-
这个问题与嵌入式系统编程无关。重新标记。
标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid