开发者

Nested references not saved from embedded document using mongoid

开发者 https://www.devze.com 2023-04-03 01:58 出处:网络
Here a tricky issue dealing with nested documents, references and embedded documents. You can find a zip file containing a sample project illustrating the issue.

Here a tricky issue dealing with nested documents, references and embedded documents. You can find a zip file containing a sample project illustrating the issue.

I'm Using Rails v3.0.9 Mongo v1.8.3 Mongoid v2.2.0

Models
  • Conversation
  • Message
  • Text
  • Media
  • Link

  • A Conversation embeds_many Messages (autosave => true)

  • A Message has_many Media (autosave => true)
  • Text inherit from Message (STI)
  • Link inherit from Link (STI)
  • A Conversation accept_nested_attributes_for :messages
  • A Message accept_nested_attributes_for :medias

In rails console, let's do some testing :

ruby-1.9.2-p180 :001 > c = Conversation.new({:messages_attributes => [{'_type' => 'Text', :content => 'content', :medias_attributes => [{'_type' => 'Link', :url =&g开发者_Python百科t; '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

Now if you change the relations between Conversation <=> Message as follow :

  • A Conversation has_many Messages

It works perfectly. Some rails console testing also :

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">]

Here is the link to the sample zip file : http://d.pr/oMUc


Well, not so easy on this one ^^

However here is the link to the git issue for mongoid : https://github.com/mongoid/mongoid/issues/1216

For people who have the same issue !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号