0
我将我的项目从mysql迁移到mongodb并且有一些麻烦。在mongoid中更新嵌入式文档的最佳方法
如何以ActiveRecord的方式更新嵌入式文档中的属性?在mongoid中可能吗?
class Template
include Mongoid::Document
include Mongoid::Versioning
embedded_in :site, :inverse_of => :templates
end
class Site
embeds_many :templates
end
例如:
site = Site.find(params[:current_site_id])
template = site.templates.find(params[:id])
template.update_attributes(params[:template])
我:
Access to the collection for Template is not allowed since it is an embedded document, please access a collection from the root document.
好的,但如何从根文件做呢?
编辑:
它看起来像Mongoid ::版本,我在模板中使用使一些麻烦。我删除包含的模块,一切工作正常。
您使用的是哪种版本的mongoid? –
gem'mongoid','2.0.0' –
由于模板是一个嵌入式文档,如果您将'Mongoid :: Versioning'添加到'Site'模型 - 它将对嵌入文档的所有更改进行版本化。由于嵌入式文档实际上是母文档的一部分,而不是关系。 – theTRON