ruby-on-railsruby-on-rails-5paper-trail-gem

paper_trail gem saving versions with object_changes nil


We just started using the PaperTrail gem and have noticed that 75% of the records in the versions table have nil for the object_changes column. Any idea why this would be happening and how we can stop it?

Using Rails 5.1 and PaperTrail 10.1.


Solution

  • The nil object changes is due to touch events on attributes that were skipped. The only solution I came up with for that is to only track versions on create, update and destroy.

    I also discovered that we had duplicate version records. We turned on PaperTrail for all models by putting the below in ApplicationRecord, this caused duplicate versions to be created if a class inherited from another one. ie If you have class Foo < Bar and do Bar.create that would create 2 identical version records.

    Initial Version in ApplicationRecord

    def self.inherited(subclass)
      super
      subclass.send(:has_paper_trail)
    end
    

    Final Version

    def self.inherited(subclass)
      classes_to_skip = %w[Foo]
      attributes_to_skip = [:bar_at]
      on_actions = [:create, :update, :destroy]
    
      super
      unless classes_to_skip.include?(subclass.name)
        subclass.send(:has_paper_trail, on: on_actions, ignore: attributes_to_skip)
      end
    end