Skip to content

Instantly share code, notes, and snippets.

@sjha4
Created March 22, 2023 02:47
Show Gist options
  • Save sjha4/164a8dd3e29b795b0318f39ba6772d3e to your computer and use it in GitHub Desktop.
Save sjha4/164a8dd3e29b795b0318f39ba6772d3e to your computer and use it in GitHub Desktop.
Override Default Audits in your model
class MyModel < ApplicationRecord
# Custom audit function to create audit records
def create_audit_record(action)
Audit.create!(
auditable_id: self.id,
auditable_type: self.class.name,
action: action,
user_id: current_user.id # replace with your authentication method
)
end
# Override create method to create audit record
def create
super
create_audit_record('create')
end
# Override update method to create audit record
def update(attributes)
super
create_audit_record('update')
end
# Override destroy method to create audit record
def destroy
create_audit_record('destroy')
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment