1

I'm using the gem Audited in my Rails app. I want to create a way to print out the Audit record so I can have a feed of what has been changed by a User, or what has changed on an object. Currently, I have setup a method called print_audit on each object that I'm using with Audited. But this seems incredibly wrong. I would like to override the to_s method on the Audit model... but I'm unsure what the correct way of doing that would be... is the approach incorrect and if not how would I go about doing this?

jon snow
  • 3,062
  • 1
  • 19
  • 31
daveomcd
  • 6,367
  • 14
  • 83
  • 137

2 Answers2

2

There is now a more straighforward method to accomplish this, as defined here in the official documentation. Here is a snippet that would add to_s to each Audit instance:

# config/initializers/audited.rb
class CustomAudit < Audited::Audit
  def to_s
    "This overrides the default to_s method!"
  end
end

Audited.config do |config|
  config.audit_class = CustomAudit
end
s_dolan
  • 1,196
  • 1
  • 9
  • 21
1

I've used the following method as I find it much cleaner.

Added file: config/initializers/audited_additional_methods.rb

module Audited
  module Adapters
    module ActiveRecord
      class Audit < ::ActiveRecord::Base
        def to_s
          "Added my new to_s logic here"
        end
      end
    end
  end
end
daveomcd
  • 6,367
  • 14
  • 83
  • 137