5

私は次のモデル構造を持っています: aBanner 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

いくつかのコンテンツを含む 1 つのスライドを含むバナーから始めています。ここで、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' を同時に更新することはできません」

しかし、新しいスライドを作成し、既存のスライドに新しいコンテンツを一度に追加することは許可されていると確信しています。

4

0 に答える 0