Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With paper_trail gem how to skip changes between nil and blank?

TL;DR: How can one skip changes between nil and blank in paper_trail?

I use https://github.com/airblade/paper_trail to log data changes in several ActiveRecord objects. Some changes are useless from my customers' point of view, so I need to ignore all changes between nil and blank (empty string).

paper_trail doesn't seem to have such option. You can skip certain fields with :skip option. Can you pass in a Proc that would "decide" to skip changes conditionally?

like image 836
Alex Kovshovik Avatar asked Oct 29 '22 22:10

Alex Kovshovik


1 Answers

Your can add a proc to define a new version should be saved or not, the code should be:

class ExampleModel < ActiveRecord::Base
  has_paper_trail :if => Proc.new { |t| t.attr_1.present? && t.attr_2.present? }
end

By the way :unless was also supported! This works for PaperTrail >= 4.0

like image 152
Hieu Pham Avatar answered Nov 15 '22 07:11

Hieu Pham