audited, Look up audits belonging to a user
Asked Answered
E

3

6

I am using audited to track changes for a model called Page. I would like to be able to find all audits associated with a certain user (via user_id in the audits table).

How can I do that? So far, the only way I have found to access the Audit model is like this:

@audits = Audited::Adapters::ActiveRecord::Audit.all

Which just doesn't seem like it's the right way to do things.

Trying @audits = Audit.all gives an Uninitialized constant error.

Is there a more graceful way of interacting with models provided by gems?

Elocution answered 8/7, 2012 at 13:58 Comment(0)
L
7

Maybe something like

include Audited::Adapters::ActiveRecord::Audit

and then you can do

@audits = Audit.all

?

I think that should work... Or better yet:

include Audited

Linesman answered 8/7, 2012 at 15:8 Comment(2)
Yes this works include Audited, see a simple example: class Audit < ActiveRecord::Base include Audited endEssene
it's worth noting that you can do Audited.audit_class which then allows you to do things like Audited.audit_class.where("...")Faxon
T
3

You can access all Audit records with

Audited::Audit.all

I got the result when I typed the

Audited.audit_class

DEPRECATION WARNING: audit_class is deprecated and will be removed from Rails 5.0 (Audited.audit_class is now always Audited::Audit. This method will be removed.).

Toucan answered 7/10, 2016 at 5:50 Comment(0)
G
0

I know this isn't an efficient way to do so, but this is how I do it.

In a Rails console I retrieve a record which I know is audited.

@page = Page.first

I then retrieve that record's first audit.

@audit = @page.audits.first

You can then call #class on @audit

@audit.class

Result:

Audited::Adapters::ActiveRecord::Audit(id: integer, created_at: 
datetime, updated_at: datetime, auditable_id: integer, auditable_type: 
string, user_id: integer, user_type: string, username: string, action: 
string, audited_changes: text, version: integer, comment: string, 
full_model: text, remote_address: string, associated_id: integer, 
associated_type: string, request_uuid: string)

Audited::Adapters::ActiveRecord::Audit is the class name, which you can then use in your search.

audits = Audited::Adapters::ActiveRecord::Audit.where(:user_id => 8675309)

Georama answered 3/8, 2017 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.