audited icon indicating copy to clipboard operation
audited copied to clipboard

acts_as_tenant support?

Open jasper502 opened this issue 3 years ago • 2 comments

My app uses multi tenancy with https://github.com/ErwinM/acts_as_tenant. Each model in the tenant has an account_id column and the current tenant id is automatically added on commit.

I tried adding an account_id column and then a model:

class Audit < ApplicationRecord
  acts_as_tenant :account
end

I hoped that the account_id would magically get added but not. Is there another method to set this before commit other than the :after_audit callback? Looking to avoid a second transaction on every call here. I can access the id from current_account.id. It's also saved in the audited record.

jasper502 avatar Jan 05 '22 04:01 jasper502

I am trying this for now:

def after_audit
  self.audits.last.update!(account_id: self.account_id || nil)
end

jasper502 avatar Jan 05 '22 15:01 jasper502

Probably a few approaches, but one is to use a custom audit class: https://github.com/collectiveidea/audited#custom-audit-model

# config/initializers/audited.rb
Audited.config do |config|
  config.audit_class = CustomAudit
end
class CustomAudit < Audited::Audit
   acts_as_tenant :account
end

calebtocco avatar Jan 11 '22 22:01 calebtocco