Using current_user in a controller results in a "users" query that includes an unnecessary ORDER BY

The method current_user from devise will end up running a query like the following:

SELECT  "users".*
FROM "users"
WHERE "users"."id" = 1
ORDER BY "users"."id" DESC
LIMIT 1;

Here the ORDER BY is redundant (and technically the LIMIT too since there can only be 1 row with a given ID).

This particular query appears to be the result of what devise does to get the current user. At some point it ends up in orm_adapter-0.5.0/lib/orm_adapter/adapters/active_record.rb line 16:

# @see OrmAdapter::Base#get
def get(id)
  klass.where(klass.primary_key => wrap_key(id)).first
end

In Rails first will add a ORDER BY and a LIMIT, resulting in the above SQL query.

The impact of this is relatively minor, but if possible we should get rid of the ORDER BY since it's not necessary.

Edited Jun 26, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading