【问题标题】:Audited-activerecord gem not working properly for polymorphic associationsAudited-activerecord gem 对多态关联不能正常工作
【发布时间】:2016-07-17 08:57:05
【问题描述】:

我想像我一样对嵌套关联进行审计

用户拥有多个地址,即任一

家庭地址或办公地址

现在,如果我只有一个表地址,并且我已经使用类型和 id 来区分它们。在这种情况下,如果我对 User 使用 associated_audits,那么它将只创建一个审计记录,并且每当我再次更新记录时,它只是替换上一次审计与最后一次审计。

这是模型关联:

class Patient <  ActiveRecord::Base

  devise :database_authenticatable, :registerable, :recoverable, :rememberable,
         :trackable, :validatable, :confirmable, request_keys: [:subdomain]

  has_one :home_address,-> { where(addr_type: 'home') },class_name: 'Address'
  has_one :office_address,-> { where(addr_type: 'office') }, class_name: 'Address'
  has_associated_audits

  accepts_nested_attributes_for :home_address, allow_destroy: true
  accepts_nested_attributes_for :office_address,  allow_destroy: true
end


class Address < ActiveRecord::Base

  belongs_to :patient
  audited

end

【问题讨论】:

  • "在这种情况下,如果我为 User 使用 associated_audits,那么它只会创建一个审计记录"审计或地址?如果您需要帮助,请发布您的模型和架​​构,不要只是假设人们会知道您在说什么
  • 我已经提到了细节。现在您可以使用家庭地址或办公室地址的嵌套属性更新患者,并且可以检查每次更新患者时是否只有一次审核
  • 如果您仍然遇到上面的错误,您可能想尝试更改您的type 列。它是 rails 中的保留名称,经常会导致错误
  • 是的,我知道,这就是我使用 addr_type 的原因。但我认为这不是原因。我认为问题可能是针对 has_one 实施的关联审计进行审计。
  • #Rocco 你有没有在你的任何应用程序中实现过这个?还是只是传递建议?

标签: ruby-on-rails ruby activerecord audit


【解决方案1】:
class Address < ActiveRecord::Base
  belongs_to :patient
  audited associated_with: :patient
end

class Patient <  ActiveRecord::Base
  has_many :addresses
  has_associated_audits
end

【讨论】:

  • 但是如果我们有两个以上的关联审计,它也会对其他模型审计进行分组
  • 其实我不想用has_many :addresses。病人只有一个办公地址和一个家庭住址。此外,当我们对任何特定列使用 audited 时,它会在运行迁移时中断。
猜你喜欢
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2012-04-10
  • 2016-11-03
  • 1970-01-01
相关资源
最近更新 更多