collectiveidea / audited

Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.
MIT License
3.34k stars 645 forks source link

Associated models two levels deep #676

Open camerondrysdale opened 1 year ago

camerondrysdale commented 1 year ago

We have a model structure like this in our app:

class Product < ApplicationRecord
  has_one :menu

  audited
  has_associated_audits
end

class Menu < ApplicationRecord
  belongs_to :product
  has_many :menu_items

  audited associated_with: :product
  has_associated_audits
end

class MenuItem < ApplicationRecord
  belongs_to :menu

  audited associated_with: :menu
end

And we want to see audits information against our Product when either the Menu or MenuItem is updated. We can see the Menu changes being displayed in the audits, but the MenuItem updates aren't coming through so seems it doesn't see them as an association through the Menu associated_with.

Does the Audited gem only support one-level deep associations? Or do we need to link the MenuItem to the Product (even though it doesn't have a direct relation to it).

camerondrysdale commented 1 year ago

It seems doing:

class MenuItem < ApplicationRecord
  belongs_to :menu

  audited associated_with: :product
end

Does indeed work... however we lose the structure of the audits then... is this the correct way to handle multiple level models in Audited?