【发布时间】:2011-06-18 14:42:42
【问题描述】:
我将项目从mysql迁移到mongodb,遇到了一些麻烦。
如何以 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.
好的,但是如何从根文档开始呢?
编辑:
我在 Template 中使用的 Mongoid::Versioning 看起来有些麻烦。我删除了包含的模块,一切正常。
【问题讨论】:
-
你用的是什么版本的mongoid?
-
由于模板是嵌入文档,如果您将
Mongoid::Versioning添加到Site模型 - 它将对嵌入文档的任何更改进行版本控制。因为嵌入文档实际上是父文档的一部分,而不是关系。
标签: ruby-on-rails mongodb mongoid