审核与Rails Audited gem的深层关联

时间:2016-12-23 15:06:16

标签: ruby-on-rails ruby acts-as-audited

我有接下来的三个模型:

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

基本上,A school有很多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包含附件审核更改。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

h311o

我认为这应该为您提供school = School.find(params[:id]) audits = school.audits + school.associated_audits school.subjects.each { |subject| audits += subject.associated_audits } SchoolSubject的整个审核集合。这是你追求的那种吗?

答案 1 :(得分:0)

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

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

company.own_and_associated_audits