Search for a position in the active recording object

I have a league table that I create with:

QuizuserAnswer.all(:include =>:user, :select => 'user_id, SUM(answer) AS points', :group =>'user_id', :order=>'points DESC' 

Returns the following object:

  => [#<QuizuserAnswer user_id: 340>, #<QuizuserAnswer user_id: 348>] 

Now I want to find the league table position of one specific user. Is there a clean way? or do i need to go through all the columns?

+6
source share
1 answer

From a very small number, I got your question, I think you want something like the following

 answers = QuizuserAnswer.all(:include =>:user, :select => 'user_id, SUM(answer) AS points', :group =>'user_id', :order=>'points DESC') user_id = 348 answers.map(&:user_id).index(user_id) #This will return 1 

ie It will return the position of the response specified by the user among the list of answers

+11
source

Source: https://habr.com/ru/post/953059/


All Articles