Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

paper_trail

> 后端框架
Open source

Track changes to your rails models

7.0K stars0 likes2 views
WebsiteGitHub

About

Track changes to your rails models

PaperTrail

[![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.

Documentation

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

Table of Contents

  • 1. Introduction
    • 1.a. Compatibility
    • 1.b. Installation
    • 1.c. Basic Usage
    • 1.d. API Summary
    • 1.e. Configuration
  • 2. Limiting What is Versioned, and When
    • 2.a. Choosing Lifecycle Events To Monitor
    • 2.b. Choosing When To Save New Versions
    • 2.c. Choosing Attributes To Monitor
    • 2.d. Turning PaperTrail Off
    • 2.e. Limiting the Number of Versions Created
  • 3. Working With Versions
    • 3.a. Reverting And Undeleting A Model
    • 3.b. Navigating Versions
    • 3.c. Diffing Versions
    • 3.d. Deleting Old Versions
    • 3.e. Queries
    • 3.f. Defunct item_ids
  • 4. Saving More Information About Versions
    • 4.a. Finding Out Who Was Responsible For A Change
    • 4.b. Associations
    • 4.c. Storing Metadata
  • 5. ActiveRecord
    • 5.a. Single Table Inheritance (STI)
    • 5.b. Configuring the versions Association
    • 5.c. Generators
    • 5.d. Protected Attributes
  • 6. Extensibility
    • 6.a. Custom Version Classes
    • 6.b. Custom Serializer
    • 6.c. Custom Object Changes
    • 6.d. Excluding the Object Column
    • 6.e. Error handling
  • 7. Testing
    • 7.a. Minitest
    • 7.b. RSpec
    • 7.c. Cucumber
    • 7.d. Spork
    • 7.e. Zeus or Spring
  • 8. PaperTrail Plugins
  • 9. Integration with Other Libraries
  • 10. Related Libraries and Ports
  • Articles
  • Problems
  • Contributors
  • Contributing
  • Inspirations
  • Intellectual Property

1. Introduction

1.a. Compatibility

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.

1.b. Installation

  1. Add PaperTrail to your Gemfile and run [bundle][57].

    gem 'paper_trail'

  2. Add a versions table to your database:

    bundle exec rails generate paper_trail:install [--with-changes] [--uuid]
    bundle exec rails db:migrate

    See section 5.c. Generators for details.

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

    ruby
    class Widget < ActiveRecord::Base
      has_paper_trail
    end
  4. If your controllers have a current_user method, you can easily track who is responsible for changes by adding a controller callback.

    ruby
    class ApplicationController
      before_action :set_paper_trail_whodunnit
    end

1.c. Basic Usage

Your models now have a versions method which returns the "paper trail" of changes to your model.

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

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

ruby
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.

ruby
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.

1.d. API Summary

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.

1.e. Configuration

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.

1.e.1 Global

Global configuration options affect all threads.

  • association_reify_error_behaviour
  • enabled
  • has_paper_trail_defaults
  • object_changes_adapter
  • serializer
  • version_limit
  • version_error_behavior

Syntax example: (options described in detail later)

ruby
# config/initializers/paper_trail.rb
PaperTrail.config.enabled = true
PaperTrail.config.has_paper_trail_defaults = {
  on: %i[create update destroy]
}
PaperTrail.config.version_limit = 3

These 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.

2. Limiting What is Versioned, and When

2.a. Choosing Lifecycle Events To Monitor

You can choose which events to track with the on option. For example, if you only want to track update events:

ruby
class Article < ActiveRecord::Base
  has_paper_trail on: [:update]
end

has_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].

The versions.event Column

Your 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.

ruby
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'

Controlling the Order of AR Callbacks

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.

ruby
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
end

The paper_trail.on_destroy method can be further configured to happen :before or :after the destroy event.

Issues· 8 open

View all issuesOpen on GitHub
  • #1555

    `#reify` returns the downcased value for `encrypts ... ignore_case: true` attributes

    Updated Sep 7, 2026
  • #1508

    Changeset values different when called from `item`.

    Updated Aug 26, 2026
  • #1545

    MySQL does not support ILIKE queries (have_a_version_with_changes, where_object_changes, other areas)

    Updated Jul 16, 2026
  • #1459

    Ignore certain columns by default

    keepUpdated Jul 14, 2025

> Tags

Rubyactiverecordauditlograils

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 22, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架