How can I count the result of an ActiveRecord .group query? Chaining .count returns a hash

前端 未结 6 1199
萌比男神i
萌比男神i 2021-01-08 00:51

I can use the following :

User.where(\"zip_code = \'48104\'\").group(\'users.id\').count

To get hashes like :

{195=>1, 1         


        
6条回答
  •  难免孤独
    2021-01-08 01:32

    Another way, useful if the values can be > 1.

    User.where("zip_code = '48104'").group('users.id').count.values.sum
    

提交回复
热议问题