Skip to content

Conversation

@edmunteanu
Copy link
Contributor

A new rule was added to the set, which aims to make memoization more robust when using ActiveRecord's find_by method.

It essentially points out cases like this (cobra) and this (fredi), where it aims to ensure that memoization is truly doing what it's intended – ensuring that the query is run only once.

After a short discussion with @coorasse, I'm not sure this subtlety is worth the double (or even worse, 5x) lines of code. Yes, some queries could be ran more than once unnecessarily depending on how often the memoized variable is used, but queries should be cached anyway, which doesn't hinder performance. For this reason, we'd suggest disabling this rule in general.

I personally feel it doesn't bring much to the table, but I'm curious if you see things differently!

Copy link
Member

@coorasse coorasse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member

@rnestler rnestler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it's a subtle thing to be aware of: That the memoization won't work if the query returned nil, but I'm also not sure if the autofix helps a lot, so in favor of disabling it.

@coorasse coorasse merged commit 5bdf6e3 into main Sep 18, 2025
1 check passed
@coorasse coorasse deleted the feature/disable-find-by-memoization branch September 18, 2025 07:53
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.

5 participants