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

Auditing models with a Composite Primary Key - possible? #700

Open robvitaro opened 3 months ago

robvitaro commented 3 months ago

Can audited support the following scenario using Rails 7.1's composite primary keys?

class Foo
  has_many :foo_bars
  audited
  has_associated_audits
end

class Bar
  has_many :foo_bars
  audited
  has_associated_audits
end

class FooBar
  self.primary_key = [:foo_id, :bar_id] # i.e., there is no "id" column
  belongs_to :foo
  belongs_to :bar

  audited associated_with: :foo
  audited associated_with: :bar
end

Currently it would give an error similar to this:

ActiveRecord::CompositePrimaryKeyMismatchError:
        Association FoobBar#audits primary key ["foo_id", "bar_id"] doesn't match with foreign key auditable_id. Please specify query_constraints, or primary_key and foreign_key values.
danielmorrison commented 3 months ago

Related to #585.

I think that'd be a great addition, but I don't personally have any projects with composite primary keys, so my motivation to add this is low. I'd gladly review a PR.