views:

49

answers:

1

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 ActiveRecord 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
.
.
.
+4  A: 

Do the following:

counts = Users.count(:group => :country)

Now print the count:

counts.each |country, count| do
 p "#{country} - #{count}"
end

# Prints
US 123
UK 54
DE 33

counts["UK"] #-> prints 123
KandadaBoggu
counts.each do |country, count| and it works for me. Thank you.
Adnan