Search code examples
ruby-on-railsaudit

Rails 4 + audited: Get audits for deleted object


I am using the Audited Gem with my project but I don't understand how to get the audit trail for a deleted object. Their example shows:

user = User.create!(name: "Steve")
user.audits.count # => 1
user.update_attributes!(name: "Ryan")
user.audits.count # => 2
user.destroy
user.audits.count # => 3

but if all I know is that a user is missing, how can I access the audit since I need access to the object that gets audited?


Solution

  • If you know the id of the user, you can try:

    Audited::Adapters::ActiveRecord::Audit.where(auditable_type: 'User', auditable_id: user_id)
    

    For specific actions like create, update, destroy, you can try their scopes - creates, updates, destroys. I found it on their github repo.