Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rectify versions on has_many/belongs_to association with paper_trail

Tags:

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?

like image 811
blawzoo Avatar asked Apr 01 '12 01:04

blawzoo


1 Answers

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.

like image 120
BookOfGreg Avatar answered Sep 28 '22 07:09

BookOfGreg