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.
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.
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.
Different types of internal audits include compliance, operational, financial and information technology audits.
Use the Audited gem to log all changes to your Rails models.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With