Track Records Changes with PaperTrail gem

PaperTrail gem

What is the PaperTrail gem?

The paper trail gem is a wonderful Ruby on Rails gem for tracking all the changes in the model’s data for editing or versioning. After upgrading papertrail gem for an application, you can use it to see how your data looks across different periods. You can take it back to any version of model data. The Ruby on Rails gem “paper_trail_ allows you to undo all the changes after a record has been destroyed so that you can restore it totally. 

Here are a few features of Paper Trail Ggem:- 

In this article, we will learn how to use papertrail gem. This guide will take you through the process of installation, and using it in a basic app.

Also Read: 10 best Ruby on Rails Gems for Web Development

Installation and Configuration of PaperTrail:

# Gemfile
gem 'paper_trail', '~> 10.3', '>= 10.3.1'

bundle exec rails generate paper_trail:install
bundle exec rake db:migrate

class User < ActiveRecord::Base
has_paper_trail
end

class User < ActiveRecord::Base
Before_action: set_paper_trail_whodunnit
end

# 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

class User< ActiveRecord::Base
has_paper_trail on: []
paper_trail.on_destroy
paper_trail.on_update
paper_trail.on_create
paper_trail.on_touch
End

class Article < ActiveRecord::Base
has_paper_trail ignore: [:phone_number]
end

class User< ActiveRecord::Base
has_paper_trail only: [:phone_number]
end

Class User < ActiveRecord::Base
has_paper_trail skip: [:image_upload]
end

PaperTrail.enabled = false

Conclusion

So far we have learned how to install PaperTrail gem and use it in a system to keep an eye on records via track. We have seen some basic methods and options given by PaperTrail which are used to. 

For more functionality of PaperTrail refer following link:

Related posts

Ruby Metaprogramming to help Rails programmers in Ruby on Rails App

by Ankur Vyas
7 years ago

Top Skills to Look for in a Ruby on Rails Developer

by Shardul Bhatt
3 years ago

Implement Ransack Gem in Ruby on Rails

by Dhaval Gandhi
7 years ago
Exit mobile version