我有以下模型结构: 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
我从一个横幅开始,上面有一张幻灯片,上面有一些内容。现在我向服务器发送一个 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'”
但我很确定我可以立即创建新幻灯片并向现有幻灯片添加新内容