ruby on rails - Counting distinct and duplicate attribute values in an array -


I have a user's array that is sorted in descending order, based on total_payets.

I need to find that each user's rank issue in that array is that more than one user can have a total number and, thus, the same rank. For example, three users can be at number three with 200 points. Here is my current code:

  class leader & lt; ActiveRecord :: Base def self.points_leaders all_leaders = all_points_leaders # all_leaders_with_rank = [] all_leaders.each do sorted by all_points in the order of all objects. User | Rank = all_lider.index (user) +1 all_leaders_with_rank & lt; & Lt; LDR.new (rank, user) #lDR is a straight and return all_leaders_with_rank end end  

How do I modify the code so that the correct rank is returned, and not only its value index status ?

Frontline-animal-force approach will be a simple change in your existing code. / P>

  rank = 1 all_leaders.each_with_index. User, idx | # If this user has a different number than the previous user in the list, then ramp # ranks. If IDX & gt; 0 and amp; Amp; All_leaders [idx - 1] .total_points! = User.total_points # The issue of using idx as an offset here is that you will end with the # t1 # t1 # t3 # in the case of earlier tie ranks = idx + 1 end all_leaders_with_rank & Lt; & Lt; Ldr.new (rank, user) #lDR is a straight end  

Comments