class AuditEvent < ApplicationRecord
validates :actor_id, :record_type, :record_id, :action, presence: true
end
module Audited
extend ActiveSupport::Concern
included do
after_update :audit_update
end
private
def audit_update
changes = saved_changes.except('updated_at')
return if changes.empty?
AuditEvent.create!(
actor_id: Current.member&.id,
record_type: self.class.name,
record_id: id,
action: 'update',
changeset: changes
)
end
end
Auditing isn’t just “save everything”. Capture who did it, what changed, and why. Rails gives you dirty tracking; store diffs in a JSON column. Keep it minimal to avoid ballooning storage.