[Proposal] add `tally` method to ActiveRecord

Hey all,

Now we’ve got the tally method available for Ruby enumerables, it would be nice to be able to call this on a model / collection, in practice analogous to Model.group(:column_name).count.

It’s very intuitive, I’d argue more so than the existing solution.

So you would get something like:

Model.tally(:column_name)
#  SELECT COUNT(*) AS count_all, "model"."column_name" AS model_column_name FROM "models" GROUP BY "models"."column_name"
# => {"value1"=>1, "value2"=>10, "value3"=>20}

What do people think?

2 Likes