Convert SQL query to Ruby help
I need to query my database table to find which employee has the most support tickets related to them. I can do this just fine using this MySQL query开发者_运维问答:
SELECT employee_id, COUNT(id) AS number_of_tickets FROM tickets GROUP BY employee_id ORDER BY number_of_tickets DESC LIMIT 1;
How would write this in Ruby-on-Rails?
Thanks very much for your assistance.
I use Ruby version 1.8.6, Rails version 2.2.2 and MySQL Server version 5.0.
Try this:
Ticket.find(:all, :select => 'employee_id, count(id) as number_of_tickets',
:group => 'employee_id' , :order => "number_of_tickets Desc", :limit => 1 )
Or directly use:
Ticket.find_by_sql('select...... ' )
精彩评论