Track changes to your rails models
[![Build Status][4]][5] [![Gem Version][53]][54] [![SemVer][55]][56]
Track changes to your models, for auditing or versioning. See how a model looked at any stage in its lifecycle, revert it to any version, or restore it after it has been destroyed.
This is the user guide. See also, the API reference.
Choose version: Unreleased, 17.0, 16.0, 15.2, 14.0, 13.0, 12.3, 11.1, 10.3, 9.2, 8.1, 7.1, 6.0, 5.2, 4.2, 3.0, 2.7, 1.6
| paper_trail | ruby | activerecord |
|---|---|---|
| unreleased | >= 3.2.0 | >= 7.1, <= 8.1 |
| 17 | >= 3.2.0 | >= 7.1, <= 8.1 |
| 16 | >= 3.0.0 | >= 6.1, <= 8.0 |
| 15.2 | >= 3.0.0 | >= 6.1, <= 7.2 |
| 15.1 | >= 3.0.0 | >= 6.1, <= 7.1 |
| 15 | >= 3.0.0 | >= 6.1, <= 7.1 |
| 14 | >= 2.7.0 | >= 6.0, < 7.1 |
| 13 | >= 2.6.0 | >= 5.2, < 7.1 |
| 12 | >= 2.6.0 | >= 5.2, < 7.1 |
| 11 | >= 2.4.0 | >= 5.2, < 6.1 |
| 10 | >= 2.3.0 | >= 4.2, < 6.1 |
| 9 | >= 2.3.0 | >= 4.2, < 5.3 |
| 8 | >= 2.2.0 | >= 4.2, < 5.2 |
| 7 | >= 2.1.0 | >= 4.0, < 5.2 |
| 6 | >= 1.9.3 | >= 4.0, < 5.2 |
| 5 | >= 1.9.3 | >= 3.0, < 5.1 |
| 4 | >= 1.8.7 | >= 3.0, < 5.1 |
| 3 | >= 1.8.7 | >= 3.0, < 5 |
| 2 | >= 1.8.7 | >= 3.0, < 4 |
| 1 | >= 1.8.7 | >= 2.3, < 3 |
Experts: to install incompatible versions of activerecord, see
paper_trail/compatibility.rb.
Add PaperTrail to your Gemfile and run [bundle][57].
gem 'paper_trail'
Add a versions table to your database:
bundle exec rails generate paper_trail:install [--with-changes] [--uuid]
bundle exec rails db:migrateSee section 5.c. Generators for details.
Add has_paper_trail to the models you want to track.
class Widget < ActiveRecord::Base
has_paper_trail
endIf your controllers have a current_user method, you can easily track who
is responsible for changes
by adding a controller callback.
class ApplicationController
before_action :set_paper_trail_whodunnit
endYour models now have 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', 'create', 'destroy'. See also: "The versions.event Column"
v.created_at
v.whodunnit # ID of `current_user`. Requires `set_paper_trail_whodunnit` callback.
widget = v.reify # The widget as it was before the update (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
widget.name # 'Doobly'
# Add has_paper_trail to Widget model.
widget.versions # []
widget.update name: 'Wotsit'
widget.versions.last.reify.name # '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 | create | update | destroy |
|---|---|---|---|
| Model Before | nil | widget | widget |
| Model After | widget | widget | nil |
PaperTrail stores the values in the Model Before row. Most other auditing/versioning plugins store the After row.
An introductory sample of common features.
When you declare has_paper_trail in your model, you get these methods:
…And a PaperTrail::Version instance (which is just an ordinary ActiveRecord
instance, with all the usual methods) has methods such as:
…This is just a sample of common features. Keep reading for more.
Many aspects of PaperTrail are configurable for individual models; typically
this is achieved by passing options to the has_paper_trail method within
a given model.
Some aspects of PaperTrail are configured globally for all models. These
settings are assigned directly on the PaperTrail.config object.
A common place to put these settings is in a Rails initializer file
such as config/initializers/paper_trail.rb or in an environment-specific
configuration file such as config/environments/test.rb.
Global configuration options affect all threads.
Syntax example: (options described in detail later)
# config/initializers/paper_trail.rb
PaperTrail.config.enabled = true
PaperTrail.config.has_paper_trail_defaults = {
on: %i[create update destroy]
}
PaperTrail.config.version_limit = 3These options are intended to be set only once, during app initialization (eg.
in config/initializers). It is unsafe to change them while the app is running.
In contrast, PaperTrail.request has various options that only apply to a
single HTTP request and thus are safe to use while the app is running.
You can choose which events to track with the on option. For example, if
you only want to track update events:
class Article < ActiveRecord::Base
has_paper_trail on: [:update]
endhas_paper_trail installs [callbacks][52] for the specified lifecycle events.
There are four potential callbacks, and the default is to install all four, ie.
on: [:create, :destroy, :touch, :update].
versions.event ColumnYour versions table has an event column with three possible values:
| event | callback |
|---|---|
| create | create |
| destroy | destroy |
| update | touch, update |
You may also have the PaperTrail::Version model save a custom string in its
event field instead of the typical create, update, destroy. PaperTrail
adds an attr_accessor to your model named paper_trail_event, and will insert
it, if present, in the event column.
a = Article.create
a.versions.size # 1
a.versions.last.event # 'create'
a.paper_trail_event = 'update title'
a.update title: 'My Title'
a.versions.size # 2
a.versions.last.event # 'update title'
a.paper_trail_event = nil
a.update title: 'Alternate'
a.versions.size # 3
a.versions.last.event # 'update'If there are other callbacks in your model, their order relative to those
installed by has_paper_trail may matter. If you need to control
their order, use the paper_trail_on_* methods.
class Article < ActiveRecord::Base
# Include PaperTrail, but do not install any callbacks. Passing the
# empty array to `:on` omits callbacks.
has_paper_trail on: []
# Add callbacks in the order you need.
paper_trail.on_destroy # add destroy callback
paper_trail.on_update # etc.
paper_trail.on_create
paper_trail.on_touch
endThe paper_trail.on_destroy method can be further configured to happen
:before or :after the destroy event.
`#reify` returns the downcased value for `encrypts ... ignore_case: true` attributes
Changeset values different when called from `item`.
MySQL does not support ILIKE queries (have_a_version_with_changes, where_object_changes, other areas)
Ignore certain columns by default