【问题标题】:Audited Gem: tracking through multiple relationshipsAudited Gem:通过多个关系进行跟踪
【发布时间】:2020-10-17 07:38:48
【问题描述】:

寻找在我的模型中设置audited associated_withhas_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(&amp;:audits)

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2016-06-17
      • 2015-03-29
      相关资源
      最近更新 更多