Search code examples
ruby-on-railsrubyacts-as-audited

Audited - get instance of auditable object


I want to extend Audit record with some more information from the auditable type instance.

I've extended Audit behavior by config/initializers/audited.rb

class AuditExtension < Audited::Audit
  before_save :resolve_association

  def resolve_association
    puts auditable_id
    puts auditable_type
    x = auditable_type.constantize.find(auditable_id)
    ...
  end
end

Audited.config do |config|
  config.audit_class = AuditExtension
end

Is there any other way how can I get the instance of the class which is audited except the way of x initialization?


Solution

  • You can call #auditable on the Audit instance you have to access the object that was audited (e.g. Audited::Audit.first.auditable). Since this is a callback you should have access to it.