7

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'

This returns an object like this:

 => [#<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 have to loop through al columns?

flyte321
  • 421
  • 1
  • 7
  • 15

1 Answers1

13

From a very little i got your question i think you want something like 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

i.e It will return the position of the answer given by the user among the answer's list

Salil
  • 46,566
  • 21
  • 122
  • 156
  • @flyte321 :- Cheer's, remember one thing it will return `nil` if `user_id` is not present in `answers` – Salil Sep 03 '13 at 13:08
  • This actually solved my issue of trying to generate a "next" button in an action record collection with a scope – dannio Jan 13 '15 at 07:52