group by country with ActiveRecords in Rails

Posted by Adnan on Stack Overflow See other posts from Stack Overflow or by Adnan
Published on 2010-03-12T11:40:57Z Indexed on 2010/03/12 11:47 UTC
Read the original article Hit count: 184

Hello,

I have a table with users:

name  | country | 
..    | UK      |
..    | US      |
..    | US      |
..    | UK      |
..    | FR      |
..    | FR      |
..    | UK      |
..    | UK      |
..    | DE      |
..    | DE      |
..    | UK      |
..    | CA      |
.
.

What is the most efficient way with ActiveRecords to get the list of countries in my view and for each country how many users are from, so:

US 123
UK 54
DE 33
.
.
.

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about ruby-on-rails