我已经审核(以前为acts_as_audited)设置和工作。user_id 已成功保存在审计表中,但我无法找到保存tenant_id 的有效方法(我有带范围的多租户设置)。我曾尝试使用自述文件中描述的关联审计技术,但这对我不起作用。
我目前的解决方案是在每个模型中使用after_audit回调(可以用 Rails 关注点实现)来获取最后的审计并保存tenant_id:
def after_audit
audit = Audit.last
audit.tenant_id = self.tenant_id
audit.save!
end
虽然这可行,但必须再次查询审计然后更新它似乎效率低下。在保存之前将tenant_id添加到审计中对我来说更有意义,但我不知道如何做到这一点。是否可以在保存之前将tenant_id添加到审核中?如果是,那么如何?
编辑:
我也尝试在我的审计模型中包含我的默认租户范围,但它似乎没有被调用:
审计.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
编辑:2/1/16
我仍然没有对此实施解决方案,但是我目前的想法是使用:
#model_name.rb
def after_audit
audit = self.audits.last
audit.business_id = self.business_id
audit.save!
end
在这段代码中,我们得到了当前模型的最后一次审计。这样我们只处理当前模型,没有机会将审计添加到另一个业务(据我所知)。我会将此代码添加到关注点中以使其保持干燥。
我仍然无法让正常的 Rails 回调在 Audit 模型中工作。我目前看到的唯一另一种方法是分叉和修改 gem 源代码。