【问题标题】:Best way to update embedded document in mongoid在 mongoid 中更新嵌入文档的最佳方法
【发布时间】: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


【解决方案1】:

有一个 pb 带有 mongoid 的嵌入式文档版本控制,如果您使用偏执狂模式也是如此。 我建议为您的嵌入式文档停用这些选项,并在您确实需要时自行复制这些功能。

亚历克斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2011-08-19
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多