Mongoid,同时更新父母和孩子

Ahm*_*yaa 5 single-table-inheritance nested-attributes mongoid4 ruby-on-rails-4.1

我有以下模型结构:a Banner embeds_many Slides和eachSlide 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
Run Code Online (Sandbox Code Playgroud)

我开始使用一张带有一些内容的幻灯片的横幅.现在我向服务器发送一个JSON请求,将新内容添加到现有幻灯片中,并创建包含其内容的新幻灯片; 就像是

'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' }
      ]
    }
  ]
 }
Run Code Online (Sandbox Code Playgroud)

现在打电话banner.update banner_params给我一些非常奇怪的错误:

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"
Run Code Online (Sandbox Code Playgroud)

虽然这是一个不言自明的错误:

失败,错误16837:"无法同时更新'banner.slides.0.contents'和'banner.slides'"

但我很确定我可以立即创建新幻灯片并向现有幻灯片添加新内容