one_of_oldest_users = User.desc(:age).limit(1).first
This will give you one of the users with the greatest age (if there are several). If you want to get them all, the easiest way is to use two passes.
max_age = User.max(:age) oldest_users = User.where(age: max_age)
To make these queries efficient, you'll need an index on :age
, of course.
source share