JP.
JP.

Reputation: 5594

Getting a rank from ActiveRecord

If Users have points, how can I get the user rank, assuming standard positioning:

require 'active_record'

class User < ActiveRecord::Base
  def rank
    # ???
  end
end

User.all
# => [<User id:1 points:100>, <User id:2 points:400>, <User id:3 points:100>, <User id:4 points:250>]

User.find_by_id(2).rank
# => 1
User.find_by_id(3).rank
# => 3
User.find_by_id(1).rank
# => 3

I get the distinct impression this will be database intensive. I'm not too worried about that, but obviously solutions which require less processing power are winners!

If giving two users with the same number of points the same rank is too complex, I'm prepared to accept that user 1 above may be rank 3, and user 3 may be rank 4.

EDIT:

I'm actually storin gmy scores in a seperate class - as each event warrants a certain amount of points.

class Event < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :events

  def points
    events.sum(:amount)
  end

  def rank
    # This seems a bit ham-handed
    Event.count_by_sql(['select count(*) from (select sum(amount) as points from events group by user_id) where points > ?',points]) + 1
  end
end

Any ideas as to how to make this more succinct?

Thanks in advance

Upvotes: 2

Views: 2526

Answers (1)

Dogbert
Dogbert

Reputation: 222278

You can count the number of users having less points, and add one.

def rank
  User.where("points > ?", points).count + 1
end

This would return 3 for users 1 and 3.

Upvotes: 12

Related Questions