Acts As Versioned Plugin

The acts as versioned plugin allows you to manage different versions of a model record so that you can revert to a previous version or create new ones. To install execute the following command:

script/plugin install

Make your model act as versioned.

class Post < ActiveRecord::Base

Versioned post are saved in a separate database table. You can create the versioned table that will contained older posts by invoking the following statement.


Normally you can create and drop (via the drop_versioned_table function) the version tables in a ActiveRecord Migration. A side effect of invoking the create_versioned_table is that a new version integer column will be added to your original posts table. The version column will be added automatically by the create_versioned_table.

There is a slight issue with this plugin that I should note. If you have a table posts, you can’t have a post column, you will get a ActiveRecord::AssociateionTypeMismatch error if you do. This has been an issue with my models that I haven’t used it as much as I would like. I don’t want to use this plugin until I, or most likely someone else, solves this. If you want to or need to use this plugin you will need to rename the post column to something else.

So once you have your tables correctly setup, you can start creating versions of your post just by saving your them.

post = => 'My Post')

Every time you save a record of your model a copy of that instance will be stored in the post_versioned table, even if no attributes or date has been modified from the current version.

To find the current version of a post you can use the version propterty.


To revert to a previous version use the revert_to method on a post instance.

post.revert_to(1) # original post

You can save a previous version as the current on by using the save method. The save on a reverted posts will just create a new version.

Technorati Tags: , , , , , ,

One Response to “Acts As Versioned Plugin”

Leave a Reply