Skip to content

Conversation

@r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Aug 11, 2024

It is common to see code that attempts to memoize find_by result by ||=, but find_by may return nil, in which case it is not memoized as intended.

def current_user
  @current_user ||= User.find_by(id: session[:user_id])
end

In most cases, a query cache is used, but even so, performance degradation due to unnecessary processing and object creation can still occur.

See also:


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@Earlopain
Copy link
Contributor

Earlopain commented Aug 11, 2024

I like this, its a common pitfall I've encountered many times myself and is most likely not what the programmer intended.

Let's add this as a test (works fine already, no offense):

@current_user ||= User.find_by(id: session[:user_id]) || User.anon
@current_user ||= session[:user_id] ? User.find_by(id: session[:user_id]) : nil

and this pattern (also fine, correct autocorrect):

@current_user ||= User.find_by(id: session[:user_id]) unless session[:user_id].nil?

For what I presume would be the most common case, I'd write the correction as follows:

def current_user
  @current_user ||= User.find_by(id: session[:user_id])
end

# =>

def current_user
  return @current_user if instance_variable_defined?(:@current_user)

  @current_user = User.find_by(id: session[:user_id])
end

I just find it easier to parse than the if/else assignment to a single variable, but that only really works when the memoization is the only line of code, so eh.


I would exclude controllers from being inspected by this cop. They execute once and don't benefit from changing the code.. Well, actions wouldn't but those aren't the only methods a controller can have. I guess my thought here doesn't really work out.

@koic
Copy link
Member

koic commented Aug 15, 2024

If this is a new cop, consider making a corresponding update to the Rails Style Guide.

Can you open to the Rails Style Guide first?

@r7kamura
Copy link
Contributor Author

Thanks, I just proposed a pull request to rubocop/rails-style-guide:

@koic koic merged commit e8bba50 into rubocop:master Aug 7, 2025
@KieranP
Copy link

KieranP commented Aug 10, 2025

Just updated. The same is true of a lot of belongs_to or has_one methods, e.g. @country ||= @user.country where country fires an SQL query to fetch the record, but could be nil. But without type analysis, I imagine this would be impossible to detect.

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.

4 participants