2011-01-28 5 views
1

私はここで何が欠けていますか?私がしなければMongoid、埋め込みドキュメントでバージョン管理を行うことはできませんか?

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. 

すべては、私がコンテンツを介してこれを行う場合、それは動作しません。今すぐOK

ある

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' 

私は何ですか?ここで間違っていますか?!?

おかげで、 アレックス

答えて

0

はMongoid ::バージョン管理とMongoid ::パラノイアは、残念ながら、現在、埋め込まれた文書では動作しません。

0

私は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 
関連する問題