Mongoid,同时更新父母和孩子

时间:2015-02-01 04:59:22

标签: nested-attributes single-table-inheritance ruby-on-rails-4.1 mongoid4

我有以下模型结构: 一个Banner embeds_many Slides和每个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

我开始时的横幅上有一张带有一些内容的幻灯片。 现在我向服务器发送一个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' }
      ]
    }
  ]
 }

现在调用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"

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

  

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

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

0 个答案:

没有答案
相关问题