2014-01-14 3 views
5

필자는 PaperTrail이 관리하는 버전이있는 City 클래스를 사용하고 있습니다. 내 시스템에는 Devise에서 관리하는 사용자가 있습니다. City 클래스의 특정 버전을 편집 한 사용자와 관계를 맺고 싶습니다.Rails 3, PaperTrail 및 Whodunnit 사용자 인스턴스

내가 레일 3.2.15 및 용지 트레일 3.0.0 사용하여 다음과 같은 구성 시도 이렇게하려면 :이

class City < ActiveRecord::Base 
    has_paper_trail 
end 

class User < ActiveRecord::Base 
    has_many :editions, :foreign_key => 'whodunnit', :class_name => "PaperTrail::Version" 
end 

class PaperTrail::Version < ActiveRecord::Base 
    belongs_to :user 
end 

을 내가 할 수있는 예를 들어, 특정 사용자에 의해 만들어 에디션이 있습니다

User.last.editions 최종 사용자의 버전 목록 (PaperTrail :: Version objects)을 가져옵니다.

는 그럼 난 은 첫 번째시의 마지막 버전을 편집 한 사용자의 사용자 인스턴스를 얻기 위해, 예를 들어, 특정 도시를 수정했습니다 사용자의 예를을 가지고 간단한 것 생각 :

# NoMethodError: undefined method `user' for #<PaperTrail::Version:0x000000051d0da8> 
City.first.versions.last.user 

이렇게되지 않으면 PaperTrail :: Version의 "user"메소드에 액세스 할 수 있도록 모델을 구성해야합니까?

Ps .: 필자는 모델 PaperTrail :: Version을 만들어야한다고 확신하지는 않지만 그 방향으로 다른 방법을 찾지 못했습니다.

답변

11

답을 직접 찾았습니다. 모든 것은 PaperTrail :: Version을 서브 클래 싱하고 : class_name 옵션을 사용한다.

class City < ActiveRecord::Base 
    has_paper_trail, :class_name => 'Version' 
end 

class User < ActiveRecord::Base 
    has_many :editions, :foreign_key => 'whodunnit', :class_name => "Version" 
end 

class Version < PaperTrail::Version 
    belongs_to :user, :foreign_key => 'whodunnit' 
end 

그리고 특정 버전에 대한 사용자를 호출 할 때 지금은 사용자 탐정 소설 인스턴스를

City.first.versions.last.user 
=> #<User id: 2, email: "[email protected]", encrypted_password: ...> 
+0

완벽 반환 : 내 작업 구성입니다! 이것은 기본적으로 papertrail에서 작동해야합니다! – Flezcano

+1

paper_trail의 v4.0.0.beta2에서'has_paper_trail : class_name => '버전'은'has_paper_trail' 다음에 쉼표없이 작동합니다. – Nobu