【发布时间】:2020-10-17 07:38:48
【问题描述】:
寻找在我的模型中设置audited associated_with 和has_associated_audits 的正确方法更容易跟踪。
设置: Publisher 拥有 Authors 的独家经销商。一个作者可以写很多本书。一本书可以有很多作者。一本书有一种类型的书籍装订。一本书装订可以有很多本书。
目标: 我希望能够调用 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,这为我提供了与作者相关的审核。
对于我做的书: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 的 ???? 部分以进行跟踪。我知道审计表的多态设置只允许它一次跟踪一个关联记录。那么这有可能吗?
【问题讨论】:
标签: ruby-on-rails polymorphism audit acts-as-audited