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

Cannot update Child and Parent at the same time

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • None

      I have the following model structure: a page embeds_one Banner that embeds_many Slides and each Slide embeds_many contents

      class Page
        include Mongoid::Document
      
        embeds_one :banner
      
        accepts_nested_attributes_for :banner, allow_destroy: true 
      end
      
      class Banner
        include Mongoid::Document
        
        embedded_in :page
        embeds_many :slides
      
        accepts_nested_attributes_for :slides, allow_destroy: true 
      end
      
      class Slide
        include Mongoid::Document
      
        embedded_in :banner
        embeds_many :contents
      
        accepts_nested_attributes_for :contents, allow_destroy: true 
      end
      
      class Content
        include Mongoid::Document
      
        embedded_in :slide
      
        field :value, type: String
      end
      
      

      and I am starting with a banner that has one slide with some contents. Now I send a JSON request to the server to add new contents to the existing slide and create new slide with its contents; something like

      
      'page' => {
         '_id' => '1',
        'banner_attributes' => {
          '_id' => '123',
          'slides_attributes' => [
            {
              '_id' => '1',
              'contents_attributes' => [
                { '_id' => '1', 'value' => 'some persisted value' },
                { 'value' => 'new content here, there is no _id yet' }
              ]
            },
            {
              'contents_attributes' => [
                { 'value' => 'new content in a newly created slide' }
              ]
            }
          ]
         }
      }
      
      

      Now calling page.update page_params give me some very strange error:

      Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
         @length=87
         @request_id=594
         @response_to=0
         @op_code=2004
         @flags=[]  
         @full_collection_name="web_builder_development.$cmd"
         @skip=0  
         @limit=-1
         @selector={:getlasterror=>1, :w=>1}
         @fields=nil>
      
      failed with error 16837: "Cannot update 'banner.slides.0.contents' and 'banner.slides' at the same time"
      
      

      while this is a self-explanatory error:

      > failed with error 16837: "Cannot update 'banner.slides.0.contents' and 'banner.slides' at the same time"

      but I should be allowed to create new slides and add new contents to existing slides at once

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            ElBayaa ElBayaa [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: