Track changes to your models' data. Good for auditing or versioning.
Ruby JavaScript HTML
Pull request Compare This branch is 22 commits ahead, 913 commits behind airblade:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

PaperTrail Build Status Dependency Status

PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed.

There's an excellent RailsCast on implementing Undo with Paper Trail.


  • Stores every create, update and destroy (or only the lifecycle events you specify).
  • Does not store updates which don't change anything.
  • Allows you to specify attributes (by inclusion or exclusion) which must change for a Version to be stored.
  • Allows you to get at every version, including the original, even once destroyed.
  • Allows you to get at every version even if the schema has since changed.
  • Allows you to get at the version as of a particular time.
  • Option to automatically restore has_one associations as they were at the time.
  • Automatically records who was responsible via your controller. PaperTrail calls current_user by default, if it exists, but you can have it call any method you like.
  • Allows you to set who is responsible at model-level (useful for migrations).
  • Allows you to store arbitrary model-level metadata with each version (useful for filtering versions).
  • Allows you to store arbitrary controller-level information with each version, e.g. remote IP.
  • Can be turned off/on per class (useful for migrations).
  • Can be turned off/on per request (useful for testing with an external service).
  • Can be turned off/on globally (useful for testing).
  • No configuration necessary.
  • Stores everything in a single database table by default (generates migration for you), or can use separate tables for separate models.
  • Supports custom version classes so different models' versions can have different behaviour.
  • Supports custom name for versions association.
  • Thoroughly tested.
  • Threadsafe.


Works with ActiveRecord 4 and ActiveRecord 3. Note: this code is on the master branch and tagged v3.x.

Version 2 is on the branch named 2.7-stable and is tagged v2.x, and works with Rails 3.

The Rails 2.3 code is on the rails2 branch and tagged v1.x. These branches are both stable with their respective versions of Rails but will not have new features added/backported to them.


Rails 3 & 4

  1. Add PaperTrail to your Gemfile.

    gem 'paper_trail', '~> 3.0.3'

  2. Generate a migration which will add a versions table to your database.

    bundle exec rails generate paper_trail:install

  3. Run the migration.

    bundle exec rake db:migrate

  4. Add has_paper_trail to the models you want to track.


In order to configure PaperTrail for usage with Sinatra, your Sinatra app must be using ActiveRecord 3 or 4. It is also recommended to use the Sinatra ActiveRecord Extension or something similar for managing your applications ActiveRecord connection in a manner similar to the way Rails does. If using the aforementioned Sinatra ActiveRecord Extension, steps for setting up your app with PaperTrail will look something like this:

  1. Add PaperTrail to your Gemfile.

    gem 'paper_trail', '~> 3.0.3'

  2. Generate a migration to add a versions table to your database.

    bundle exec rake db:create_migration NAME=create_versions

  3. Copy contents of create_versions.rb into the create_versions migration that was generated into your db/migrate directory.

  4. Run the migration.

    bundle exec rake db:migrate

  5. Add has_paper_trail to the models you want to track.

PaperTrail provides a helper extension that acts similar to the controller mixin it provides for Rails applications.

It will set PaperTrail.whodunnit to whatever is returned by a method named user_for_paper_trail which you can define inside your Sinatra Application. (by default it attempts to invoke a method named current_user)

If you're using the modular Sinatra::Base style of application, you will need to register the extension:

# bleh_app.rb
require 'sinatra/base'

class BlehApp < Sinatra::Base
  register PaperTrail::Sinatra

API Summary

When you declare has_paper_trail in your model, you get these methods:

class Widget < ActiveRecord::Base
  has_paper_trail   # you can pass various options here

# Returns this widget's versions.  You can customise the name of the association.

# Return the version this widget was reified from, or nil if it is live.
# You can customise the name of the method.

# Returns true if this widget is the current, live one; or false if it is from a previous version.

# Returns who put the widget into its current state.

# Returns the widget (not a version) as it looked at the given timestamp.

# Returns the widget (not a version) as it was most recently.

# Returns the widget (not a version) as it became next.

# Generates a version for a `touch` event (`widget.touch` does NOT generate a version)

# Turn PaperTrail off for all widgets.

# Turn PaperTrail on for all widgets.

# Check wheter PaperTrail is enabled for all widgets

And a PaperTrail::Version instance has these methods:

# Returns the item restored from this version.
version.reify(options = {})

# Return a new item from this version
version.reify(dup: true)

# Returns who put the item into the state stored in this version.

# Returns who changed the item from the state it had in this version.

# Returns the next version.

# Returns the previous version.

# Returns the index of this version in all the versions.

# Returns the event that caused this version (create|update|destroy).

In your controllers you can override these methods:

# Returns the user who is responsible for any changes that occur.
# Defaults to current_user.

# Returns any information about the controller or request that you want
# PaperTrail to store alongside any changes that occur.

Basic Usage

PaperTrail is simple to use. Just add 15 characters to a model to get a paper trail of every create, update, and destroy.

class Widget < ActiveRecord::Base

This gives you a versions method which returns the paper trail of changes to your model.

>> widget = Widget.find 42
>> widget.versions             # [<PaperTrail::Version>, <PaperTrail::Version>, ...]

Once you have a version, you can find out what happened:

>> v = widget.versions.last
>> v.event                     # 'update' (or 'create' or 'destroy')
>> v.whodunnit                 # '153'  (if the update was via a controller and
                               #         the controller has a current_user method,
                               #         here returning the id of the current user)
>> v.created_at                # when the update occurred
>> widget = v.reify            # the widget as it was before the update;
                               # would be nil for a create event

PaperTrail stores the pre-change version of the model, unlike some other auditing/versioning plugins, so you can retrieve the original version. This is useful when you start keeping a paper trail for models that already have records in the database.

>> widget = Widget.find 153
>>                                 # 'Doobly'

# Add has_paper_trail to Widget model.

>> widget.versions                             # []
>> widget.update_attributes :name => 'Wotsit'
>>             # 'Doobly'
>> widget.versions.last.event                  # 'update'

This also means that PaperTrail does not waste space storing a version of the object as it currently stands. The versions method gives you previous versions; to get the current one just call a finder on your Widget model as usual.

Here's a helpful table showing what PaperTrail stores:

Event Model Before Model After
create nil widget
update widget widget'
destroy widget nil

PaperTrail stores the values in the Model Before column. Most other auditing/versioning plugins store the After column.

Choosing Lifecycle Events To Monitor

You can choose which events to track with the on option. For example, to ignore create events:

class Article < ActiveRecord::Base
  has_paper_trail :on => [:update, :destroy]

You may also have the PaperTrail::Version model save a custom string in it's event field instead of the typical create, update, destroy. PaperTrail supplies a custom accessor method called paper_trail_event, which it will attempt to use to fill the event field before falling back on one of the default events.

>> a = Article.create
>> a.versions.size                           # 1
>> a.versions.last.event                     # 'create'
>> a.paper_trail_event = 'update title'
>> a.update_attributes :title => 'My Title'
>> a.versions.size                           # 2
>> a.versions.last.event                     # 'update title'
>> a.paper_trail_event = nil
>> a.update_attributes :title => "Alternate"
>> a.versions.size                           # 3
>> a.versions.last.event                     # 'update'

Choosing When To Save New Versions

You can choose the conditions when to add new versions with the if and unless options. For example, to save versions only for US non-draft translations:

class Translation < ActiveRecord::Base
  has_paper_trail :if     => { |t| t.language_code == 'US' },
                  :unless => { |t| t.type == 'DRAFT'       }

Choosing Attributes To Monitor

You can ignore changes to certain attributes like this:

class Article < ActiveRecord::Base
  has_paper_trail :ignore => [:title, :rating]

This means that changes to just the title or rating will not store another version of the article. It does not mean that the title and rating attributes will be ignored if some other change causes a new PaperTrail::Version to be created. For example:

>> a = Article.create
>> a.versions.length                         # 1
>> a.update_attributes :title => 'My Title', :rating => 3
>> a.versions.length                         # 1
>> a.update_attributes :title => 'Greeting', :content => 'Hello'
>> a.versions.length                         # 2
>> a.previous_version.title                  # 'My Title'

Or, you can specify a list of all attributes you care about:

class Article < ActiveRecord::Base
  has_paper_trail :only => [:title]

This means that only changes to the title will save a version of the article:

>> a = Article.create
>> a.versions.length                         # 1
>> a.update_attributes :title => 'My Title'
>> a.versions.length                         # 2
>> a.update_attributes :content => 'Hello'
>> a.versions.length                         # 2
>> a.previous_version.content                # nil

The :ignore and :only options can also accept Hash arguments, where the :

class Article < ActiveRecord::Base
  has_paper_trail :only => [:title => { |obj| !obj.title.blank? } ]

This means that if the title is not blank, then only changes to the title will save a version of the article:

>> a = Article.create
>> a.versions.length                         # 1
>> a.update_attributes :content => 'Hello'
>> a.versions.length                         # 2
>> a.update_attributes :title => 'My Title'
>> a.versions.length                         # 3
>> a.update_attributes :content => 'Hai'
>> a.versions.length                         # 3
>> a.previous_version.content                # "Hello"
>> a.update_attributes :title => 'Dif Title'
>> a.versions.length                         # 4
>> a.previous_version.content                # "Hai"

Passing both :ignore and :only options will result in the article being saved if a changed attribute is included in :only but not in :ignore.

You can skip fields altogether with the :skip option. As with :ignore, updates to these fields will not create a new PaperTrail::Version. In addition, these fields will not be included in the serialized version of the object whenever a new PaperTrail::Version is created.

For example:

class Article < ActiveRecord::Base
  has_paper_trail :skip => [:file_upload]

Reverting And Undeleting A Model

PaperTrail makes reverting to a previous version easy:

>> widget = Widget.find 42
>> widget.update_attributes :name => 'Blah blah'
# Time passes....
>> widget = widget.previous_version  # the widget as it was before the update
>>                       # reverted

Alternatively you can find the version at a given time:

>> widget = widget.version_at(  # the widget as it was one day ago
>>                            # reverted

Note version_at gives you the object, not a version, so you don't need to call reify.

Undeleting is just as simple:

>> widget = Widget.find 42
>> widget.destroy
# Time passes....
>> widget = PaperTrail::Version.find(153).reify  # the widget as it was before destruction
>>                         # the widget lives!

In fact you could use PaperTrail to implement an undo system, though I haven't had the opportunity yet to do it myself. However Ryan Bates has!

Navigating Versions

You can call previous_version and next_version on an item to get it as it was/became. Note that these methods reify the item for you.

>> live_widget = Widget.find 42
>> live_widget.versions.length           # 4 for example
>> widget = live_widget.previous_version # => widget == live_widget.versions.last.reify
>> widget = widget.previous_version      # => widget == live_widget.versions[-2].reify
>> widget = widget.next_version          # => widget == live_widget.versions.last.reify
>> widget.next_version                   # live_widget

If instead you have a particular version of an item you can navigate to the previous and next versions.

>> widget = Widget.find 42
>> version = widget.versions[-2]    # assuming widget has several versions
>> previous = version.previous
>> next =

You can find out which of an item's versions yours is:

>> current_version_number = version.index    # 0-based

Finally, if you got an item by reifying one of its versions, you can navigate back to the version it came from:

>> latest_version = Widget.find(42).versions.last
>> widget = latest_version.reify
>> widget.version == latest_version    # true

You can find out whether a model instance is the current, live one -- or whether it came instead from a previous version -- with live?:

>> widget = Widget.find 42
>>                        # true
>> widget = widget.previous_version
>>                        # false

Finding Out Who Was Responsible For A Change

If your ApplicationController has a current_user method, PaperTrail will store the value it returns in the version's whodunnit column. Note that this column is of type String, so you will have to convert it to an integer if it's an id and you want to look up the user later on:

>> last_change = widget.versions.last
>> user_who_made_the_change = User.find last_change.whodunnit.to_i

You may want PaperTrail to call a different method to find out who is responsible. To do so, override the user_for_paper_trail method in your controller like this:

class ApplicationController
  def user_for_paper_trail
    logged_in? ? : 'Public user'  # or whatever

In a console session you can manually set who is responsible like this:

>> PaperTrail.whodunnit = 'Andy Stewart'
>> widget.update_attributes :name => 'Wibble'
>> widget.versions.last.whodunnit              # Andy Stewart

You can avoid having to do this manually by setting your initializer to pick up the username of the current user from the OS, like this:

# config/initializers/paper_trail.rb

# the following line is required for PaperTrail >= 3.0.3 with Rails

if defined?(::Rails::Console)
  PaperTrail.whodunnit = "#{`whoami`.strip}: console"
elsif File.basename($0) == "rake"
  PaperTrail.whodunnit = "#{`whoami`.strip}: rake #{ARGV.join ' '}"

Sometimes you want to define who is responsible for a change in a small scope without overwriting value of PaperTrail.whodunnit. It is possible to define the whodunnit value for an operation inside a block like this:

>> PaperTrail.whodunnit = 'Andy Stewart'
>> widget.whodunnit('Lucas Souza') do
>>   widget.update_attributes :name => 'Wibble'
>> end
>> widget.versions.last.whodunnit              # Lucas Souza
>> widget.update_attributes :name => 'Clair'
>> widget.versions.last.whodunnit              # Andy Stewart
>> widget.whodunnit('Ben Atkins') { |w| w.update_attributes :name => 'Beth' } # this syntax also works
>> widget.versions.last.whodunnit              # Ben Atkins

A version's whodunnit records who changed the object causing the version to be stored. Because a version stores the object as it looked before the change (see the table above), whodunnit returns who stopped the object looking like this -- not who made it look like this. Hence whodunnit is aliased as terminator.

To find out who made a version's object look that way, use version.originator. And to find out who made a "live" object look like it does, use originator on the object.

>> widget = Widget.find 153                    # assume widget has 0 versions
>> PaperTrail.whodunnit = 'Alice'
>> widget.update_attributes :name => 'Yankee'
>> widget.originator                           # 'Alice'
>> PaperTrail.whodunnit = 'Bob'
>> widget.update_attributes :name => 'Zulu'
>> widget.originator                           # 'Bob'
>> first_version, last_version = widget.versions.first, widget.versions.last
>> first_version.whodunnit                     # 'Alice'
>> first_version.originator                    # nil
>> first_version.terminator                    # 'Alice'
>> last_version.whodunnit                      # 'Bob'
>> last_version.originator                     # 'Alice'
>> last_version.terminator                     # 'Bob'

Custom Version Classes

You can specify custom version subclasses with the :class_name option:

class PostVersion < PaperTrail::Version
  # custom behaviour, e.g:
  self.table_name = :post_versions

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'

This allows you to store each model's versions in a separate table, which is useful if you have a lot of versions being created.

If you are using Postgres, you should also define the sequence that your custom version class will use:

class PostVersion < PaperTrail::Version
  self.table_name = :post_versions
  self.sequence_name = :post_version_id_seq

Alternatively you could store certain metadata for one type of version, and other metadata for other versions.

If you only use custom version classes and don't use PaperTrail's built-in one, on Rails >= 3.2 you must:

  • either declare the PaperTrail::Version class to be abstract like this (in an initializer):
# config/initializers/paper_trail.rb

# the following line is required for PaperTrail >= 3.0.3 with Rails

PaperTrail::Version.module_eval do
  self.abstract_class = true
  • or create a versions table in the database so Rails can instantiate the PaperTrail::Version superclass.

You can also specify custom names for the versions and version associations. This is useful if you already have versions or/and version methods on your model. For example:

class Post < ActiveRecord::Base
  has_paper_trail :versions => :paper_trail_versions,
                  :version  => :paper_trail_version

  # Existing versions method.  We don't want to clash.
  def versions
  # Existing version method.  We don't want to clash.
  def version


I haven't yet found a good way to get PaperTrail to automatically restore associations when you reify a model. See here for a little more info.

If you can think of a good way to achieve this, please let me know.

Has-One Associations

PaperTrail can restore :has_one associations as they were at (actually, 3 seconds before) the time.

class Location < ActiveRecord::Base
  belongs_to :treasure

class Treasure < ActiveRecord::Base
  has_one :location

>> treasure.amount                  # 100
>> treasure.location.latitude       # 12.345

>> treasure.update_attributes :amount => 153
>> treasure.location.update_attributes :latitude => 54.321

>> t = treasure.versions.last.reify(:has_one => true)
>> t.amount                         # 100
>> t.location.latitude              # 12.345

The implementation is complicated by the edge case where the parent and child are updated in one go, e.g. in one web request or database transaction. PaperTrail doesn't know about different models being updated "together", so you can't ask it definitively to get the child as it was before the joint parent-and-child update.

The correct solution is to make PaperTrail aware of requests or transactions (c.f. Efficiency's transaction ID middleware). In the meantime we work around the problem by finding the child as it was a few seconds before the parent was updated. By default we go 3 seconds before but you can change this by passing the desired number of seconds to the :has_one option:

>> t = treasure.versions.last.reify(:has_one => 1)  # look back 1 second instead of 3

If you are shuddering, take solace from knowing PaperTrail opts out of these shenanigans by default. This means your :has_one associated objects will be the live ones, not the ones the user saw at the time. Since PaperTrail doesn't auto-restore :has_many associations (I can't get it to work) or :belongs_to (I ran out of time looking at :has_many), this at least makes your associations wrong consistently ;)

Has-Many-Through Associations

PaperTrail can track most changes to the join table. Specifically it can track all additions but it can only track removals which fire the after_destroy callback on the join table. Here are some examples:

Given these models:

class Book < ActiveRecord::Base
  has_many :authorships, :dependent => :destroy
  has_many :authors, :through => :authorships, :source => :person

class Authorship < ActiveRecord::Base
  belongs_to :book
  belongs_to :person
  has_paper_trail      # NOTE

class Person < ActiveRecord::Base
  has_many :authorships, :dependent => :destroy
  has_many :books, :through => :authorships

Then each of the following will store authorship versions:

>> @book.authors << @dostoyevsky
>> @book.authors.create :name => 'Tolstoy'
>> @book.authorships.last.destroy
>> @book.authorships.clear

But none of these will:

>> @book.authors.delete @tolstoy
>> @book.author_ids = [,]
>> @book.authors = []

Having said that, you can apparently get all these working (I haven't tested it myself) with this patch:

# In config/initializers/active_record_patch.rb
module ActiveRecord
  # = Active Record Has Many Through Association
  module Associations
    class HasManyThroughAssociation < HasManyAssociation #:nodoc:
      alias_method :original_delete_records, :delete_records

      def delete_records(records, method)
        method ||= :destroy
        original_delete_records(records, method)

See issue 113 for a discussion about this.

There may be a way to store authorship versions, probably using association callbacks, no matter how the collection is manipulated but I haven't found it yet. Let me know if you do.

There has been some discussion of how to implement PaperTrail to fully track HABTM associations. See pull 90 for an implementation that has worked for some.

Storing metadata

You can store arbitrary model-level metadata alongside each version like this:

class Article < ActiveRecord::Base
  belongs_to :author
  has_paper_trail :meta => { :author_id  => :author_id,
                             :word_count => :count_words,
                             :answer     => 42 }
  def count_words

PaperTrail will call your proc with the current article and store the result in the author_id column of the versions table.

N.B. You must also:

  • Add your metadata columns to the versions table.
  • Declare your metadata columns using attr_accessible. (If you are using ActiveRecord 3, or ActiveRecord 4 with the ProtectedAttributes gem)

For example:

# config/initializers/paper_trail.rb

# the following line is required for PaperTrail >= 3.0.3 with Rails

module PaperTrail
  class Version < ActiveRecord::Base
    attr_accessible :author_id, :word_count, :answer

Why would you do this? In this example, author_id is an attribute of Article and PaperTrail will store it anyway in a serialized form in the object column of the version record. But let's say you wanted to pull out all versions for a particular author; without the metadata you would have to deserialize (reify) each version object to see if belonged to the author in question. Clearly this is inefficient. Using the metadata you can find just those versions you want:

PaperTrail::Version.where(:author_id => author_id)

Note you can pass a symbol as a value in the meta hash to signal a method to call.

You can also store any information you like from your controller. Just override the info_for_paper_trail method in your controller to return a hash whose keys correspond to columns in your versions table. E.g.:

class ApplicationController
  def info_for_paper_trail
    { :ip => request.remote_ip, :user_agent => request.user_agent }

Remember to add those extra columns to your versions table and use attr_accessible ;)

NOTE FOR RAILS 4: If you're using Strong Parameters in Rails 4 and have not included the protected_attributes gem, there's no need to declare your metadata columns using attr_accessible.

Diffing Versions

There are two scenarios: diffing adjacent versions and diffing non-adjacent versions.

The best way to diff adjacent versions is to get PaperTrail to do it for you. If you add an object_changes text column to your versions table, either at installation time with the rails generate paper_trail:install --with-changes option or manually, PaperTrail will store the changes diff (excluding any attributes PaperTrail is ignoring) in each update version. You can use the version.changeset method to retrieve it. For example:

>> widget = Widget.create :name => 'Bob'
>> widget.versions.last.changeset                # {'name' => [nil, 'Bob']}
>> widget.update_attributes :name => 'Robert'
>> widget.versions.last.changeset                # {'name' => ['Bob', 'Robert']}
>> widget.destroy
>> widget.versions.last.changeset                # {}

Note PaperTrail only stores the changes for creation and updates; it doesn't store anything when an object is destroyed.

Please be aware that PaperTrail doesn't use diffs internally. When I designed PaperTrail I wanted simplicity and robustness so I decided to make each version of an object self-contained. A version stores all of its object's data, not a diff from the previous version. This means you can delete any version without affecting any other.

To diff non-adjacent versions you'll have to write your own code. These libraries may help:

For diffing two strings:

  • htmldiff: expects but doesn't require HTML input and produces HTML output. Works very well but slows down significantly on large (e.g. 5,000 word) inputs.
  • differ: expects plain text input and produces plain text/coloured/HTML/any output. Can do character-wise, word-wise, line-wise, or arbitrary-boundary-string-wise diffs. Works very well on non-HTML input.
  • diff-lcs: old-school, line-wise diffs.

For diffing two ActiveRecord objects:

Turning PaperTrail Off/On

Sometimes you don't want to store changes. Perhaps you are only interested in changes made by your users and don't need to store changes you make yourself in, say, a migration -- or when testing your application.

You can turn PaperTrail on or off in three ways: globally, per request, or per class.


On a global level you can turn PaperTrail off like this:

>> PaperTrail.enabled = false

For example, you might want to disable PaperTrail in your Rails application's test environment to speed up your tests. This will do it (note: this gets done automatically for RSpec and Cucumber, please see the Testing section):

# in config/environments/test.rb
config.after_initialize do
  PaperTrail.enabled = false

If you disable PaperTrail in your test environment but want to enable it for specific tests, you can add a helper like this to your test helper:

# in test/test_helper.rb
def with_versioning
  was_enabled = PaperTrail.enabled?
  PaperTrail.enabled = true
    PaperTrail.enabled = was_enabled

And then use it in your tests like this:

test "something that needs versioning" do
  with_versioning do
    # your test

Per request

You can turn PaperTrail on or off per request by adding a paper_trail_enabled_for_controller method to your controller which returns true or false:

class ApplicationController < ActionController::Base
  def paper_trail_enabled_for_controller
    request.user_agent != 'Disable User-Agent'

Per class

If you are about change some widgets and you don't want a paper trail of your changes, you can turn PaperTrail off like this:

>> Widget.paper_trail_off!

And on again like this:

>> Widget.paper_trail_on!

Per method call

You can call a method without creating a new version using without_versioning. It takes either a method name as a symbol:

@widget.without_versioning :destroy

Or a block:

@widget.without_versioning do
  @widget.update_attributes :name => 'Ford'

Using a custom serializer

By default, PaperTrail stores your changes as a YAML dump. You can override this with the serializer config option:

>> PaperTrail.serializer = MyCustomSerializer

A valid serializer is a module (or class) that defines a load and dump method. These serializers are included in the gem for your convenience:

Limiting the number of versions created per object instance

If you are wary of your versions table growing to an unwieldy size, or just don't care to track more than a certain number of versions per object, there is a configuration option that can be set to cap the number of versions saved per object. Note that this value must be numeric, and it only applies to versions other than create events (which will always be preserved if they are stored).

# will make it so that a maximum of 4 versions will be stored for each object
# (the 3 most recent ones plus a `create` event)
>> PaperTrail.config.version_limit = 3
# disables/removes the version limit
>> PaperTrail.config.version_limit = nil

Deleting Old Versions

Over time your versions table will grow to an unwieldy size. Because each version is self-contained (see the Diffing section above for more) you can simply delete any records you don't want any more. For example:

sql> delete from versions where created_at < 2010-06-01;
>> PaperTrail::Version.delete_all ["created_at < ?", 1.week.ago]


You may want to turn PaperTrail off to speed up your tests. See the Turning PaperTrail Off/On section above for tips on usage with Test::Unit.


PaperTrail provides a helper that works with RSpec to make it easier to control when PaperTrail is enabled during testing. By default, PaperTrail will be turned off for all tests. When you wish to enable PaperTrail for a test you can either wrap the test in a with_versioning block, or pass in :versioning => true option to a spec block, like so:

describe "RSpec test group" do
  it 'by default, PaperTrail will be turned off' do
    PaperTrail.should_not be_enabled

  with_versioning do
    it 'within a `with_versioning` block it will be turned on' do
      PaperTrail.should be_enabled

  it 'can be turned on at the `it` or `describe` level like this', :versioning => true do
    PaperTrail.should be_enabled

The helper will also reset the PaperTrail.whodunnit value to nil before each test to help prevent data spillover between tests. If you are using PaperTrail with Rails, the helper will automatically set the PaperTrail.controller_info value to {} as well, again, to help prevent data spillover between tests.

There is also a be_versioned matcher provided by PaperTrail's RSpec helper which can be leveraged like so:

class Widget < ActiveRecord::Base

describe Widget do
  it { should_not be_versioned }

  describe "add versioning to the `Widget` class" do
    before(:all) do
      class Widget < ActiveRecord::Base

    it { should be_versioned }

It is also possible to do assertions on the versions using have_a_version_with matcher

 describe '`have_a_version_with` matcher' do
    before do
      widget.update_attributes!(:name => 'Leonard', :an_integer => 1 )
      widget.update_attributes!(:name => 'Tom')
      widget.update_attributes!(:name => 'Bob')

    it "is possible to do assertions on versions" do
       widget.should have_a_version_with :name => 'Leonard', :an_integer => 1
       widget.should have_a_version_with :an_integer => 1
       widget.should have_a_version_with :name => 'Tom'


PaperTrail provides a helper for Cucumber that works similar to the RSpec helper. By default, PaperTrail will be turned off for all scenarios by a before hook added by the helper. When you wish to enable PaperTrail for a scenario, you can wrap code in a with_versioning block in a step, like so:

Given /I want versioning on my model/ do
  with_versioning do
    # PaperTrail will be turned on for all code inside of this block

The helper will also reset the PaperTrail.whodunnit value to nil before each test to help prevent data spillover between tests. If you are using PaperTrail with Rails, the helper will automatically set the PaperTrail.controller_info value to {} as well, again, to help prevent data spillover between tests.


If you wish to use the RSpec or Cucumber helpers with Spork, you will need to manually require the helper(s) in your prefork block on your test helper, like so:

# spec/spec_helper.rb

require 'spork'

Spork.prefork do
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'paper_trail/frameworks/rspec'
  require 'paper_trail/frameworks/cucumber'

Zeus or Spring

If you wish to use the RSpec or Cucumber helpers with Zeus or Spring, you will need to manually require the helper(s) in your test helper, like so:

# spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'paper_trail/frameworks/rspec'

Testing PaperTrail

Paper Trail has facilities to test aganist Postgres, Mysql and SQLite. To switch between DB engines you will need to export the DB Variable for the engine you wish to test aganist.

Though be aware we do not have the abilty to create the db's (except sqlite) for you. You can look at .travis.yml before_script for an example of how to create the db's needed.

export DB=postgres
export DB=mysql
export DB=sqlite # this is default



Please use GitHub's issue tracker.


Many thanks to:


Intellectual Property

Copyright (c) 2011 Andy Stewart ( Released under the MIT licence.