0

我有接下来的三个模型:

class School < ActiveRecord::Base
  audited
  has_associated_audits

  has_many :subjects, dependent: :destroy
end

class Subject < ActiveRecord::Base
  audited associated_with: :school
  has_associated_audits

  has_many :attachments, as: :attachable, dependent: :destroy
end

class Attachment < ActiveRecord::Base
  audited associated_with: :attachable
  belongs_to :attachable, polymorphic: true
end

基本上, Aschool有很多subjects,每个subject都有很多attachmentsattachment模型是多态的,因为它也用于其他模型,以防万一它很重要......)

问题是审计没有像我预期的那样工作。我创建了一所学校,然后为该学校创建了一个主题,然后我为该主题添加了附件。这是我从控制台得到的:

School.last.associated_audits # => returns only changes on Subjects, not on subject's attachments.
Subject.last.associated_audits # => returns only changes associated to its attachments

但我也需要School.last.associated_audits包括附件审核的更改。

有任何想法吗?

4

2 回答 2

4

虽然您无法从深度关联中检索审计,但您可以将审计分组在一起。例如:

school = School.find(params[:id])
audits = school.audits + school.associated_audits
school.subjects.each { |subject| audits += subject.associated_audits }

我相信这应该为您提供对 和 的全部School审计Subject集合Attachments。这就是你所追求的吗?

于 2017-01-20T17:35:56.943 回答
1

经过审核的 gem 现在有一个辅助方法:

您可以一次性访问记录自己的审核和相关审核:

company.own_and_associated_audits
于 2020-12-14T20:39:30.537 回答