I have Audited (formerly acts_as_audited) setup and working. The user_id is successfully saved in the audit table but I can't figure out an efficient way to save the tenant_id (I have multitenancy setup with scopes). I have tried using the Associated Audits technique described in the README but that doesn't work for me.
My current solution is to use the after_audit callback in every model (can be implemented with Rails concerns) to get the last audit and save the tenant_id:
def after_audit
audit = Audit.last
audit.tenant_id = self.tenant_id
audit.save!
end
Whilst this works it seems like it would be inefficient to have to query for the audit again and then update it. It would make more sense to me to add the tenant_id to the audit before it saves but I can't figure out how to do this. Is it possible to add the tenant_id to the audit before saving? If yes, then how?
EDIT:
I've also tried including my default tenant scope in my Audit model but it does not seem to be called:
audit.rb
class Audit < ActiveRecord::Base
default_scope { where(tenant_id: Tenant.current_id) }
application_controller.rb
class ApplicationController < ActionController::Base
around_action :scope_current_tenant
def scope_current_tenant
Tenant.current_id = current_tenant.id
yield
ensure
Tenant.current_id = nil
end
EDIT: 2/1/16
I still haven't implemented a solution to this however my current thoughts would be to use:
#model_name.rb
def after_audit
audit = self.audits.last
audit.business_id = self.business_id
audit.save!
end
In this code we get the last audit for the current model. This way we are only dealing with the current model, there is no chance of adding the audit to another business (as far as I can tell). I would add this code into a concern to keep it DRY.
I still can't get normal Rails callbacks to work within the Audit model. The only other way I see at the moment is to fork and modified the gem source code.