Refine search results using the rails audited gem to filter based on audited changes



I utilize the ‘audited’ gem to monitor modifications made to a record. The structure of an audit object is as follows…

  id: 1,
  auditable_id: 1,
  auditable_type: "Customer",
  associated_id: nil,
  associated_type: nil,
  user_id: nil,
  user_type: nil,
  username: nil,
  action: "create",
    {"first_name"=>"Milan McClure",
     "last_name"=>"Wilfred Carter IV",
     "email"=>"[email protected]",
  version: 1,
  comment: nil,
  remote_address: nil,
  request_uuid: "6e462bf8-4788-4ec5-a6ee-f31a7a8226d0",
  created_at: Wed, 12 Apr 2017 12:20:39 UTC +00:00>,

I would like to filter out audits that specifically include a change in the first name. Is there a way to query this?


If you are utilizing PostgreSQL and the audited_changes can be assigned a json data type, you can execute the query.

Retrieve all the Consumer records from the Audited::Audit table where the ‘first_name’ attribute in the ‘audited_changes’ column is not null.

For a specific object, retrieve all the Consumer records that have been audited and have a non-null ‘first_name’ field, with the auditable_type set to “Consumer” and the auditable_id matching the given consumer_id.

Since audited_changes only includes the keys that were modified during the creation or update of an audit object.

Frequently Asked Questions