我有经审计)设置和工作。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添加到审计中吗?如果是,那怎么做?
编辑:
我也尝试过在审计模型中包括默认的租户范围,但它似乎没有被调用:
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编辑: 2/1/16
我还没有实现解决这个问题的方法,但是我目前的想法是:
#model_name.rb
def after_audit
audit = self.audits.last
audit.business_id = self.business_id
audit.save!
end在这段代码中,我们得到了当前模型的最后一次审计。这样我们只处理当前的模型,没有机会将审计添加到另一个业务(据我所知)。我会将这段代码添加到一个关注点中,以保持它的干爽。
我仍然无法让正常的Rails回调在审计模型中工作。我目前看到的唯一其他方法是分叉并修改gem源代码。
发布于 2021-08-29 18:58:34
我最近添加了一个Rails应用程序作为租户创业板,运行的是Audited。我也遇到了同样的问题。我增加了
acts_as_tenant :account审计模式但它什么也没做。我了解到,您不能在审计模型中重写,但必须创建一个继承自定义审计模型的自定义审计模型。所以我创建了模型: custom_audit.rb
class CustomAudit < Audited::Audit
acts_as_tenant :account
end然后,我将初始化器文件audited.rb添加到confi/ initializer中,如下所示:
Audited.config do |config|
config.audit_class = CustomAudit
end除了show_audit视图之外,我的所有多租户都在工作,这仍然是我遇到的问题。最后,我从测试设置中的两个租户中删除了所有审计。啊,真灵!我现在可以增加新的审计,它们的范围很好。但是我仍然需要将实际的客户机数据库合并为一个,而且我不想丢失审计表中的历史记录.不知道怎么解决这个问题。
因此,当我试图访问审计时,它会失败,因为current_tenant为零。不确定为什么要删除表中的所有当前记录来修复它,但我需要找到一种绕过它的方法。
发布于 2020-02-10 18:14:59
我的任务是实现审计,并添加一个对Org的引用。迁移添加了以下一行:
t.references :org, type: :uuid, index: true, null: true为了保存org_id,我最终编写了一个初始化器- audited.rb。该文件如下所示:
Rails.configuration.after_initialize do
Audited.audit_class.class_eval do
belongs_to :org, optional: true
default_scope MyAppContext.context_scope
before_create :ensure_org
private
def ensure_org
return unless auditable.respond_to? :org_id
self.org_id = auditable.org_id
end
end
end希望这能有所帮助!
https://stackoverflow.com/questions/27145771
复制相似问题