简体   繁体   English

如果分数相同,则RAILS 3.2中的排名相同

[英]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.

相关问题 Rails 3.2以相同形式渲染关联的模型 - Rails 3.2 Rendering Associated Models in Same Form Rails 3.2具有相同模型名称的多个数据库连接 - Rails 3.2 multiple database connections with same model name 替换同一属性上的Rails 3.2范围,而不是链接 - Rails 3.2 scopes on same attribute are replaced instead of chained Rails,同一段代码不适用于3.2 - Rails, same piece of code won't work with 3.2 在Rails 3.2中,多个用户递增同一计数器的最安全方法是什么 - in Rails 3.2, what is a safest way for multiple users to increment the same counter 在Rails 3.2中,如何在相同的表单上有2个链式ajax调用? - How to have 2 chained ajax calls on the same form in Rails 3.2? 如何将来自多个模型的数据添加到同一视图(Rails 3.2) - How to add data from multiple models to same view (Rails 3.2) 确保rails 3.2中has_many:through关联的父模型相同 - Ensure parent models are the same for a has_many :through association in rails 3.2 我们是否需要在rails 3.2的new / create和edit / update中添加相同的if条件? - Do we need to put the same if condition in both new/create & edit/update in rails 3.2? 使用Devise 3.2 Rails 4.1使用相同的表单初始化用户创建的用户配置文件 - Initializing user profile on user creation with the same form with Devise 3.2 Rails 4.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM