Mongoid,无法对嵌入文档进行版本控制吗?

发布于 2024-10-14 20:27:53 字数 1163 浏览 1 评论 0原文

我在这里缺少什么?

我这里有一个相对简单的结构:

Class Content 
  include Mongoid::Document 
   include Mongoid::Timestamps 
   include Mongoid::Paranoia 
    field :title 
    embeds_many :localized_contents 
 end 

 Class LocalizedContent 
   include Mongoid::Document 
   include Mongoid::Timestamps 
   include Mongoid::Paranoia 
   include Mongoid::Versioning 
    field :locale 
    field :content 
    embedded_in :content, :inverse_of => :localized_contents 
 end 

如果我这样做:

 test = LocalizeContent.new(:locale => 'en', :content => 'blah') 
 test.save 

 => ok, version = 1 

 test.content = 'blah2' 
 test.save 

 => ok, version = 2, versions.count = 1, etc. 

一切都好

现在如果我通过内容执行此操作,它不起作用

 test = Content.first.localised_contents.build(:locale => 'en', :content => 'blah') 
 test.save 

 => ok, version = 1 

 test = Content.first.localized_contents.first 
 test.content = 'blah2' 
 test.save 

 => KO, version = 1, versions.count = 0, but 
 Content.first.localized_contents.first.content == 'blah2' 

我在这里做错了什么?!?

谢谢, 亚历克斯

What am I missing here ?

I have a relative simple structure here:

Class Content 
  include Mongoid::Document 
   include Mongoid::Timestamps 
   include Mongoid::Paranoia 
    field :title 
    embeds_many :localized_contents 
 end 

 Class LocalizedContent 
   include Mongoid::Document 
   include Mongoid::Timestamps 
   include Mongoid::Paranoia 
   include Mongoid::Versioning 
    field :locale 
    field :content 
    embedded_in :content, :inverse_of => :localized_contents 
 end 

if I do:

 test = LocalizeContent.new(:locale => 'en', :content => 'blah') 
 test.save 

 => ok, version = 1 

 test.content = 'blah2' 
 test.save 

 => ok, version = 2, versions.count = 1, etc. 

All is ok

Now if I do this through Content, it does not work

 test = Content.first.localised_contents.build(:locale => 'en', :content => 'blah') 
 test.save 

 => ok, version = 1 

 test = Content.first.localized_contents.first 
 test.content = 'blah2' 
 test.save 

 => KO, version = 1, versions.count = 0, but 
 Content.first.localized_contents.first.content == 'blah2' 

What am I doing wrong here ?!?

Thanks,
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2024-10-21 20:27:53

遗憾的是,Mongoid::Versioning 和 Mongoid::Paranoia 目前无法处理嵌入文档。

Mongoid::Versioning and Mongoid::Paranoia don't work with embedded documents currently, unfortunately.

冷默言语 2024-10-21 20:27:53

我正在使用 mongo (1.9.1) & mongoid (2.7.1) 并且似乎有一种方法可以强制嵌入文档进行版本控制。

这有点 hackey - 但基本上我们更改嵌套文档,然后更新父文档的“previous_update”字段。

params = { 'env_name' => 'changeme-qa', 'machine' => {'_id' =>"51f85846f0e1801113000003", 'status' => "described#{version}" }}

env = Environment.find_with_name(params['env_name'])
result = env.machines.where(:_id => params['machine']['_id'])
machine = (result.exists?) ?  machine = result.first : nil

if machine.nil?
  raise 'failed to find machine'
else
  if machine.update_attributes(params['machine'])
    env.reload 
    # here's the magic, since we cause a change in the parent (environment) record,
    # the child records get versioned
    env['previous_update'] = env['updated_at']
    env.save
  else
    raise 'failed to save'
  end
end

I'm using mongo (1.9.1) & mongoid (2.7.1) and there seems to be a way to force embedded docs to be versioned.

This is kindof hackey - but basically we change the nested doc, then update the 'previous_update' field of the parent document.

params = { 'env_name' => 'changeme-qa', 'machine' => {'_id' =>"51f85846f0e1801113000003", 'status' => "described#{version}" }}

env = Environment.find_with_name(params['env_name'])
result = env.machines.where(:_id => params['machine']['_id'])
machine = (result.exists?) ?  machine = result.first : nil

if machine.nil?
  raise 'failed to find machine'
else
  if machine.update_attributes(params['machine'])
    env.reload 
    # here's the magic, since we cause a change in the parent (environment) record,
    # the child records get versioned
    env['previous_update'] = env['updated_at']
    env.save
  else
    raise 'failed to save'
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文