how to build audit trails in rails app

audit-trail, ruby-on-rails

Solution

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

Problem

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.

Original source

Related problems