嵌套引用未使用mongoid从嵌入文档中保存

时间:2011-09-06 08:45:18

标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid

这是处理嵌套文档,引用和嵌入文档的棘手问题。 您可以找到一个包含说明问题的示例项目的zip文件。

我正在使用 Rails v3.0.9 Mongo v1.8.3 Mongoid v2.2.0

楷模
  • 会话
  • 消息
  • 文本
  • 媒体
  • 链接

  • 对话embeds_many消息(自动保存=> true)

  • 消息has_many Media(autosave => true)
  • 文本继承自Message(STI)
  • 链接继承自Link(STI)
  • A Conversation 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&lt; =&gt;之间的关系消息如下:

  • A Conversation has_many Messages

完美无缺。 一些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

1 个答案:

答案 0 :(得分:1)

嗯,这个不太容易^^

但是这里是mongoid的git问题的链接:https://github.com/mongoid/mongoid/issues/1216

对于有同样问题的人!