Reputation: 377
So please imagine i have this sql table : User :
So until now by doing this in my controller:
@cities = User.group(:city).count
and by doing something like this in my view:
<%= "#{city} (#{count})" %>
I got this :
What i need now is to add a line which sum the count of each city like this :
I don't want to make a new sql query for this, because i'm pretty sure there is a cleaner way to do it.
I Hope that i was clear, thanks !
Upvotes: 0
Views: 5639
Reputation: 10285
You won't be able to do this in a single SQL query unless you craft some fancy SQL yourself. If performance is not a problem, I would aggregate this on the controller:
counts = User.group(:city).count
total = counts.values.sum
Upvotes: 6