Count and result

What is the equivelant of the following SQL in rails:

SELECT person, count(person) as occurance FROM invoice GROUP BY person;

This would give something like:

Person | Occurance

Bill 1
Joe 15
Carlos 7

etc etc…

It seems the fx. Person.count(…) will only yield the actual occurance,
but not listed with the actual person - and Person.find(…) will not
give the occurance. So is my only option to use find_by_sql() ?

/mich

Hi –

On Mon, 17 Jul 2006, mich wrote:

Carlos 7

etc etc…

It seems the fx. Person.count(…) will only yield the actual occurance,
but not listed with the actual person - and Person.find(…) will not
give the occurance. So is my only option to use find_by_sql() ?

How about (if I’m getting the table relations right):

Invoice.find(:all, :select => “person, count(person) as occurrence”,
:group => “person”)

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me

How about (if I’m getting the table relations right):

Invoice.find(:all, :select => “person, count(person) as occurrence”,
:group => “person”)

Ahh, of course !

Works nicely.

Thanks !

/mich

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs