【问题标题】:Mongoid, can't make versioning work with embedded document?Mongoid,不能使用嵌入式文档进行版本控制吗?
【发布时间】:2011-01-28 03:52:32
【问题描述】:

我在这里缺少什么?

我这里有一个比较简单的结构:

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. 

一切正常

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

 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' 

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

谢谢, 亚历克斯

【问题讨论】:

    标签: ruby-on-rails versioning document mongoid


    【解决方案1】:

    不幸的是,Mongoid::Versioning 和 Mongoid::Paranoia 目前不适用于嵌入式文档。

    【讨论】:

      【解决方案2】:

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

      这有点骇人听闻 - 但基本上我们会更改嵌套文档,然后更新父文档的“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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 2013-03-12
        相关资源
        最近更新 更多