Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to build audit trails in rails app

I have a small rails app. I want to put audit trail in it. Basically when a new user is added. it will insert a row in AuditTrailUsers table with the new user_id created and logged in users' user_id.

I am thinking about using rails callback before_save for this. However, I am not sure if that will work.

Imagine I have model/Users.rb and model/AuditTrailUser.rb

class User < ActiveRecord::Base
    validates_presence_of :userid, :password
        before_save :insert_audit

  def self.authenticate(userid, password)
        user = self.find_by_userid_and_password(userid, password)       
    user
  end

  ##implement insert_audit

end

How can I implement insert_audit so that it takes in a user id (of logged in user) so that it can pass it to AuditTrailUser when calling AuditTrailUser.create(...).

If I pass in logged in user's user id...then will I have to explicitly call before_save every where...

I am new to rails.

like image 962
groovynoob Avatar asked Mar 17 '10 19:03

groovynoob


People also ask

How do I enable audit trail?

To enable Audit Trail, in Setup, select Security | Security Settings, and check Enable Audit Logging Data Collection. Note Audit Trail data is available only after enabling. Data logged before enabling is not available.

What is audit log rails?

Audit logs are a set of records that document the changes made to other data in a system, along with the user who made those changes, and the time those changes were made.

What are four different types of audit trails?

Different types of internal audits include compliance, operational, financial and information technology audits.


1 Answers

Use the Audited gem to log all changes to your Rails models.

like image 116
groovynoob Avatar answered Oct 10 '22 02:10

groovynoob