Skip to content

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

@robvitaro

Description

@robvitaro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions