0

寻找正确的设置方法audited associated_withhas_associated_audits在我的模型中更容易跟踪。

设置:出版商与作者拥有独家经销商。一个作者可以写很多本书。一本书可以有很多作者。一本书有一种类型的书籍装订。一本书装订可以有很多本书。

目标:我希望能够调用 Publisher.find(1).associated_audits 让所有人一举失败。

class Publisher < ActiveRecord::Base
  audited
  has_associated_audits

  has_many :books
end

class Author < ActiveRecord::Base
  audited associated_with: :publisher
  has_associated_audits

  belongs_to :publisher
  has_many :author_to_books
  has_many :books, through: :author_to_books
end

class AuthorToBook < ActiveRecord::Base
  audited associated_with: #????? this is where I need help
  has_associated_audits

  belongs_to :book
  belongs_to :author
end

class Book < ActiveRecord::Base
  audited associated_with: #????? this is where I need help
  has_associated_audits

  has_many :author_to_books
  has_many :authors, through: :author_to_books
  belongs_to :book_binding_type
end

class BookBindingType < ActiveRecord::Base
  audited associated_with: #????? this is where I need help
  has_associated_audits

  has_many :books
  has_many :publishers, through: :books
end

目前我可以打电话Publisher.find(1).associated_audits,这给了我对 Author 的相关审计。

对于我做的书:Book.joins(:audits).references(:audits).joins(:authors).where(authors: {publisher_id: 1}).map(&:audits)

对于 BookBindingType 我做Book.joins(:audits).references(:audits).joins(:authors).where(authors: {publisher_id: 1}).map(&:audits)

正如您可以猜到的最后两个查询,同时考虑到时间复杂性......当我开始拥有更多记录时,它们仍然需要一些时间来运行。

问题:如何设置????已审核 gem 的部分以进行跟踪。我知道审计表的多态设置只允许它一次跟踪一个关联记录。那么它甚至可能吗?

4

1 回答 1

0

对于少数看过的人;我还没有找到官方associated_with:的方法。但是,我通过直接查询 Audits 表大大增加了处理时间。我最终将使用我自己的自定义审计模型扩展审计模型,但现在我正在做:

Audited::Audit.where(auditable_type: 'BookBindingType', auditable_id: BookBindingType.joins(:publishers).where(publishers: {id: 1}))

这比我在问题描述中使用的包含/连接更快地运行查询。

于 2020-06-26T19:19:45.483 回答