2011-01-28 4 views
1

무엇이 여기에 있습니까?몽고 이드, 포함 된 문서로 버전 작업을 할 수 없습니까?

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' 

내가 할 일 여기서 틀린거야?!?

덕분에, 알렉스

답변

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 
관련 문제