When I use User.count(:all, :group => "name")
, I get multiple rows, but it's not what I want. What I want is the count of the rows. How can I get it?
Currently (18.03.2014
- Rails 4.0.3
) this is correct syntax:
Model.group("field_name").count
It returns hash with counts as values e.g.
SurveyReport.find(30).reports.group("status").count
#=> {
"pdf_generated" => 56
}
User.count
will give you the total number of users and translates to the following SQL: SELECT count(*) AS count_all FROM "users"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With