Skip to content

Conversation

@rafaelsales
Copy link

@rafaelsales rafaelsales commented Mar 10, 2025

Problem

Calling Models::ActiveRecord::User.without_auditing { user.touch } generates the query below:

SELECT "audits".* FROM "audits" WHERE "audits"."auditable_id" = ? AND "audits"."auditable_type" = ? ORDER BY "audits"."version" DESC LIMIT ?

Culprit:

def audit_touch
unless (changes = audited_changes(for_touch: true, exclude_readonly_attrs: true)).empty?

Why this is a big deal

In an endpoint that requires high performance, I need to perform record.touch(:connected_at) along with some other audited record updates and make sure we're not running any extra queries.

Solution

The current code checks for auditing_enabled too deep in the callback chain. Instead, we should use guards to return in each callback as early as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant