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?
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.
ORM is Object Relational Mapper. It means you don't have to manually call the database yourself; the ORM handles it for you. Ruby on Rails uses one called ActiveRecord, and it's a really good one. ORM allows you to do things such as: User.
arel_table end. The Arel::Table object acts like a hash which contains each column on the table. The columns given by Arel are a type of Node , which means it has several methods available on it to construct queries. You can find a list of most of the methods available on Node s in the file predications.
A toolkit for building modeling frameworks like Active Record. Rich support for attributes, callbacks, validations, serialization, internationalization, and testing.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With