Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Keep a DRY history of your ActiveRecord models' changes

This branch is 2 commits ahead, 241 commits behind laserlemon:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



Finally, DRY ActiveRecord versioning!

acts_as_versioned by technoweenie was a great start, but it failed to keep up with ActiveRecord's introduction of dirty objects in version 2.1. Additionally, each versioned model needs its own versions table that duplicates most of the original table's columns. The versions table is then populated with records that often duplicate most of the original record's attributes. All in all, not very DRY.

simply_versioned by mmower started to move in the right direction by removing a great deal of the duplication of acts_as_versioned. It requires only one versions table and no changes whatsoever to existing models. Its versions table stores all of the model attributes as a YAML hash in a single text column. But we could be DRYer!

vestal_versions keeps in the spirit of consolidating to one versions table, polymorphically associated with its parent models. But it goes one step further by storing a serialized hash of only the models' changes. Think modern version control systems. By traversing the record of changes, the models can be reverted to any point in time.

And that's just what vestal_versions does. Not only can a model be reverted to a previous version number but also to a date or time!


In environment.rb: do |config|
  config.gem 'vestal_versions'

At your application root, run:

$ sudo rake gems:install

Next, generate and run the first and last versioning migration you'll ever need:

$ script/generate vestal_versions_migration
$ rake db:migrate


To version an ActiveRecord model, simply add versioned to your class like so:

class User < ActiveRecord::Base

  validates_presence_of :first_name, :last_name

  def name
    "#{first_name} #{last_name}"

It's that easy! Now watch it in action…

>> u = User.create(:first_name => 'Steve', :last_name => 'Richert')
=> #<User first_name: "Steve", last_name: "Richert">
>> u.version
=> 1
>> u.update_attribute(:first_name, 'Stephen')
=> true
=> "Stephen Richert"
>> u.version
=> 2
>> u.revert_to(:first)
=> 1
=> "Steve Richert"
>> u.version
=> 1
=> true
>> u.version
=> 3
>> u.update_attribute(:last_name, 'Jobs')
=> true
=> "Steve Jobs"
>> u.version
=> 4
>> u.revert_to!(2)
=> true
=> "Stephen Richert"
>> u.version
=> 5
NEW in this fork!!!


Lets say you have the following senario:

Patient comes into a Doctors office. Patient tells the doctor they have “XYZ insurance” The Bill is sent out and rejected because the insurance is wrong. You now need to update the existing record with an effective date before the patient's visit.

Now if you try to generate a claim you can see what the patient originally told you by using

If you want the information that should have been originally you do this:


COOL EHHH!!! (10/23/2009 => I am in the process of making this right now. I'll update ASAP.)

Something went wrong with that request. Please try again.