Ruby on rails 3 Mongoid,与带有时间戳和版本控制的嵌入式文档相混淆?

Ruby on rails 3 Mongoid,与带有时间戳和版本控制的嵌入式文档相混淆?,ruby-on-rails-3,mongodb,mongoid,Ruby On Rails 3,Mongodb,Mongoid,我已经使用Mongoid大约3个月了,多亏了这里的优秀文档和资源,我已经成功地完成了几乎所有我需要的事情 但是为了改进一些我做了一些备份的东西,我肯定在嵌入式文档方面做了很多努力 简而言之,我试图做的是维护嵌入文档的版本控制和时间戳,但我无法做到这一点 以下是我的模型的相关部分: class Content include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia embeds

我已经使用Mongoid大约3个月了,多亏了这里的优秀文档和资源,我已经成功地完成了几乎所有我需要的事情

但是为了改进一些我做了一些备份的东西,我肯定在嵌入式文档方面做了很多努力

简而言之,我试图做的是维护嵌入文档的版本控制和时间戳,但我无法做到这一点

以下是我的模型的相关部分:

class Content
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  embeds_many :localized_contents
  accepts_nested_attributes_for :localized_contents
end

class LocalizedContent
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia
  include Mongoid::Versioning

  embedded_in :content, :inverse_of => :localized_contents
end
这里没有什么真正复杂的东西,内容模型的行为一切正常,但是
LocalizedContent
模型的行为与我期望的不一样,因此我的期望要么需要纠正,要么需要帮助纠正我做错的地方

要创建新的嵌入式文档,请执行以下操作:

my_content = Content.find(params[:id])
my_content.localized_contents.build(params[:localized_content])
if parent.save
 #redirect, etc.
end
这是因为它成功地在正确的内容中创建了一个新的嵌入式文档,但是我留下的时间戳字段为零

现在,如果我尝试更新本地化的内容:

my_content = Content.find(params[:content_id])
localized_content = my_content.localized_contents.find(params[:id])
现在,如果我这样做:
localized\u content.update\u attributes(params[:localized\u content])
我会得到以下错误:

=> Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
很公平,然后我自动更新本地化内容上的字段并保存父项:

localized_content.fieldA = "value"
localized_content.fieldB = "value"
localized_content.fieldC = "value"

my_content.save
这有助于正确更新本地化内容,但: -timesteamps(udpated\u at和created\u at)仍然为零 -版本不会收到当前本地化内容的副本,并且版本不会增加

因此,正如我在本组和web上的一些论坛中多次看到的,由于性能原因,不会在嵌入文档上触发回调,因为我在父级上调用save。同样,这很公平,但正如在这些地方所建议的那样,我应该在嵌入式文档上调用save。。。但是怎么做!?!?!因为每次我这样做,我都会感到恐惧:

=> Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
更重要的是,我尝试手动回调,以便对我的embedded:localized_content.Review进行版本控制,再次出现相同的错误:

=> Mongoid::Errors::InvalidCollection: Access to the collection for LocalizedContent is not allowed since it is an embedded document, please access a collection from the root document.
我快发疯了!请帮忙。我做错了什么?如何创建和更新嵌入式文档,以便我可以调用(即使手动调用,我也不在乎)适当的回调来更新时间戳和版本控制

谢谢

亚历克斯


ps:我正在使用rails 3.0.3和mongoid 2.0.1

尝试使用创建而不是构建。EmbeddedDoc.build和EmbeddedDoc.new不会触发任何保存回调(因为还没有保存任何内容),保存父文档也不会调用嵌入的子级回调(性能决策)。EmbeddedDoc.create应该触发嵌入式文档回调

my_content = Content.find(params[:id])
puts my_content.localized_contents.create(params[:localized_content])

为了防止这个答案对任何人仍然有用,Mongoid添加了一个标记,当保存父对象时,该标记使回调在嵌入的子对象上运行

您的父对象现在应该如下所示:

class Content
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  embeds_many :localized_contents, cascade_callbacks: true
  accepts_nested_attributes_for :localized_contents
end

就这样!现在,保存父对象将在子对象上运行回调(并且
Mongoid::Timestamps
足够智能,可以只在实际更改的对象上运行)。此信息位于嵌入文档页面最底部的mongoid文档中。

为您的帖子提供合理的主题可能是获得帮助的第一步。像“请帮助”这样的东西不属于这个主题!