views:

768

answers:

3

When i use User.count(:all, :group => "name"), i got multi rows, but it's not i want, what i want is the count of the rows, how can I do it?

+2  A: 
  1. User.count will give you the total number of users and translates to the following SQL: SELECT count(*) AS count_all FROM "users"
  2. User.count(:all, :group => 'name') will give you the list of unique names, along with their counts, and translates to this SQL: SELECT count(*) AS count_all, name AS name FROM "users" GROUP BY name

I suspect you want option 1 above, but I'm not clear on what exactly you want/need.

hgimenez
thanks hgimenez, what i want is select count(*) as count_all) from (select name as name from users group by name)
In that case, what Sikachu's answer seems more appropriate - you want the number of unique names, and that'll do.
hgimenez
+1  A: 

Try using User.find(:all, :group => "name").count

Good luck!

Grant Copley
+2  A: 

Probably you want to count the distinct name of the user?

User.count(:name, :distinct => true)

would return 3 if you have user with name John, John, Jane, Joey (for example) in the database.

 ________
| name   |
|--------|
| John   |
| John   |
| Jane   |
| Joey   |
|________|
Sikachu