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?

Jay
  • 2,861
  • 3
  • 29
  • 51

3 Answers3

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

varatis
  • 14,494
  • 23
  • 71
  • 114
  • Yes this works include Audited, see a simple example: class Audit < ActiveRecord::Base include Audited end – nictrix Jan 31 '13 at 17:09
  • 2
    it's worth noting that you can do Audited.audit_class which then allows you to do things like Audited.audit_class.where("...") – user3334690 Oct 16 '14 at 14:24
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.).

V-SHY
  • 3,925
  • 4
  • 31
  • 47
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)

Tass
  • 1,628
  • 16
  • 28