5

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

class Banner
  include Mongoid::Document

  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

'banner' => {
  '_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 banner.update banner_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 am pretty sure that I am allowed to create new slides and add new contents to existing slides at once

Ahmed ElBayaa
  • 256
  • 2
  • 11

0 Answers0