Topic: get user with most posts

Hi,
  So I need to find the user who has the most posts.
Fairly easy like so:
User.all.max_by {|u| u.posts.count}

However this will return the first user found with the highest number of posts. If another user, encountered later has the same number of posts then they are not returned.
Now ideally I'd like to return nil in this instance, but the best thing I've come up with so far is below and feels like a hack...

first_user = User.all.reverse.max_by {|u| u.solvs.count}
second_user = User.all.max_by {|u| u.solvs.count}
if first_user == second_user
  return first_user
else
  return nil
end

Can anyone tell me a better way to do this?

thanks

Re: get user with most posts

I'm not quite sure I follow.  It sounds like you want all users with a maximum count.  See http://guides.rubyonrails.org/active_re … lculations

If your User has an age you can get the maximum age by

Users.maximum("aget")

max_count = Users.maximum("users.posts.count")
users = Users.where(:posts_count => max_count)

Or something like that depending on how you have your ActiveRecord relationships set up.