Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1217

Nested references not saved from embedded document

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.1
    • Affects Version/s: None
    • Component/s: None
    • None

      Hi guys,

      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 => '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

            Assignee:
            Unassigned Unassigned
            Reporter:
            gottfrois gottfrois
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: