How to create a full Audit log in Rails for every table? How to create a full Audit log in Rails for every table? ruby-on-rails ruby-on-rails

How to create a full Audit log in Rails for every table?


I had a similar requirement on a recent project. I ended using the acts_as_audited gem, and it worked great for us.

In my application controller I have line like the following

audit RunWay,RunWayModel,OtherModelName

and it takes care of all the magic, it also keeps a log of all the changes that were made and who made them-- its pretty slick.

Hope it helps


Use the Vestal versions plugin for this:

Refer to this screen cast for more details. Look at the similar question answered here recently.

Vestal versions plugin is the most active plugin and it only stores delta. The delta belonging to different models are stored in one table.

class User < ActiveRecord::Base  versionedend# following lines of code is from the readme    >> u = User.create(:first_name => "Steve", :last_name => "Richert")=> #<User first_name: "Steve", last_name: "Richert">>> u.version=> 1>> u.update_attribute(:first_name, "Stephen")=> true>> u.name=> "Stephen Richert">> u.version=> 2>> u.revert_to(10.seconds.ago)=> 1>> u.name=> "Steve Richert">> u.version=> 1>> u.save=> true>> u.version=> 3


Added this monkey-patch to our lib/core_extensions.rb

ActiveRecord::Associations::HasManyThroughAssociation.class_eval do   def delete_records(records)    klass = @reflection.through_reflection.klass    records.each do |associate|      klass.destroy_all(construct_join_attributes(associate))    end  endend

It is a performance hit(!), but satisfies the requirement and considering the fact that this destroy_all doesn't get called often, it works for our needs--though I am going to check out acts_as_versioned and acts_as_audited