How to rectify versions on has_many/belongs_to association with paper_trail
Asked Answered
N

2

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?

Noll answered 1/4, 2012 at 1:4 Comment(3)
Do you have any update on this?Heterosporous
I also have same issueBathometer
did you find the solution?Alviani
H
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.

Hirai answered 1/2, 2014 at 11:28 Comment(0)
M
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.

Mazdaism answered 23/4, 2015 at 5:54 Comment(2)
This does not answer the question at all. You just have copied and pasted the poorly written instructions on this topic.Macon
@Macon Maybe you can help me, and others, by editing the question and make it clearer what it is that the OP is asking for then?Mazdaism

© 2022 - 2024 — McMap. All rights reserved.