Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

sql - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...