22

I use paper_trail in rails to track my models versions. But the documentation on the github repo indicates that the gem doesn't support has_many, belongs_to associations.

Let's say I've an app that records the ceos names of some comapnies:

class Company < ActiveRecord::Base
  has_many :ceos
  has_paper_trail
end

class Ceo < ActiveRecord::Base
  belongs_to :companies
  has_paper_trail
end

The above example represent the information of ABC Inc.

company.name => "ABC"
company.ceo.past => "John Henry"
company.ceo.present =>  "Amy Warren"

How can I implement the following operation so it will reset the company and the company's ceos names to the last version?

Metalskin
  • 3,998
  • 5
  • 37
  • 61
blawzoo
  • 2,465
  • 4
  • 19
  • 24

2 Answers2

1

You could attempt to re-model the association to remove the has_many because in the case of CEOs, a company may have_many CEOs through its life, but it only has_one CEO for a certain period.

The implementation of this might be a has_one to a join table made up of the ID of both CEO and Company, and the time periods it was valid for.

A beneficial side effect is it would become trivial to have a person be CEO of a company 2 times with another CEO in between and have easy traversal of that in the domain.

BookOfGreg
  • 3,550
  • 2
  • 42
  • 56
0

The instructions for how to handle this can be found in the README: https://github.com/airblade/paper_trail/blob/master/README.md#associations

Basically  will need to create a version_associations table, either at installation time with the rails generate paper_trail:install --with-associations option or manually for this to work.

I suggest you read the full documentation on Github for details on how this works.