views:

39

answers:

1

Hey all,

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.

A: 

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...... ' )

Rishav Rastogi
Ok, I have been struggling with this for a little while now, but using this method produces "#". I originally thought this might be because its storing it in an array or hash, but alas, I can't work it out.Any ideas why I'm getting "#" as output instead of an integer?
Verloren
Never mind, I worked it out. Rails was outputting a memory address for a collection, all I needed to do was specify the field name I wanted to output. Thanks again for your answer!
Verloren