Roll Your Own Audit Logging

Curated ago by @jeremysmithco

Description

Here’s a simple audit logging solution using an AuditLog ActiveRecord model with a polymorphic association, a Sidekiq worker, and an audit_log controller method. I wrote about the approach in Audit Logging in Rails.

1
2
3
4
5
class AuditLogsController < ApplicationController
  def index
    @audit_logs = current_account.audit_logs.includes(:auditable, :user).order(created_at: :desc)
  end
end

This solution logs events scoped to an account, and this controller shows how you might fetch all the audit logs for that account.