Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve versioned ActiveRecord associations?

I want to work with versioned ActiveRecord associations. E.g., I want to find the object that another object belongs_to as of a certain past date, or the one that it belonged to before that. Does there already exist a library subclassing Rails' ActiveRecord to provide versioned relations? Or some other Ruby library which provides persistable versioned relations?

like image 758
Christian - Reinstate Monica C Avatar asked Nov 06 '08 17:11

Christian - Reinstate Monica C


2 Answers

Try the ActsAsVersioned plugin

like image 134
Ken Mayer Avatar answered Sep 21 '22 10:09

Ken Mayer


Provided you're not dealing with huge amounts of data, and the extra temporal dimension won't push your db over the edge, there are no major downsides to historically versioned data. Extra query complexity can be a slight pain, but it's nothing major.

In my case I wrote a rails plugin that handles versioning, it adds 5 columns to each versioned table (and helps handle querying/manipulation etc):

valid_from - datetime - the datetime that this version was created at

valid_to - datetime - the datetime that this version stopped being valid

root_id - integer - the id of the original row (that this is a subsequent version of)

created_by - integer - The user id of the user that performed the creation of this version

retired_by - integer - The user id of the user that retired this version

For currently active rows, valid_to is null. Adding an index on valid_to aids in keeping performance snappy.

like image 42
Michael Avatar answered Sep 20 '22 10:09

Michael