[英]If same score then same rank in RAILS 3.2
Hi I have a ranking system wherein if they have same score or points then both users should have same rank. 嗨,我有一个排名系统,如果他们具有相同的分数或分数,那么两个用户都应具有相同的排名。
I am getting it thru the index, but can't manage to make their indexes both equal if they have same score 我通过索引得到它,但是如果它们的分数相同,就无法使它们的索引相等
user.rb user.rb
def get_rank
x = User.get_rank.index(self)
x ? (x + 1) : x
end
def self.get_rank
Response.joins(:answer).where("answers.correct is TRUE").map(&:user).uniq.sort_by(&:score).reject{|me| me.super_admin or me.questions.count < Question.count}.reverse
end
How can I make the users who have same scores to have just 1 similar rank. 我该如何使得分相同的用户获得1个相似的排名。
Eg if both users get 25 points, and 25 is the highest from the postings, then they must have the first rank. 例如,如果两个用户均获得25分,并且帖子中最高分25分,则他们必须具有第一名。
Any workarounds will be appreciated 任何解决方法将不胜感激
The question is rather confusing but I think you could make better use of the database functions. 这个问题相当令人困惑,但是我认为您可以更好地利用数据库功能。 Maybe something like this works, since I don't know your full models, especially which object has the score of the user. 也许这样的事情可行,因为我不知道您的完整模型,尤其是哪个对象具有用户评分。 I'm assuming its on the user object: 我假设它在用户对象上:
def get_rank
scores = User.select(:score).joins(:response, :answers).where(:answers => [:correct => true]).order('score DESC').group(:score).all
# example result: [24, 22, 21, 20 ...]
rank = scores.index(score) + 1
end
The result of that statement gives you a sorted array of all actually applied scores. 该语句的结果为您提供了所有实际应用的分数的排序数组。 Since you do know the current user's score, you can get the index of that score, which is also the rank number. 由于您确实知道当前用户的分数,因此可以获得该分数的索引,这也是等级编号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.