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

前端 未结 6 1198
萌比男神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条回答
  •  旧时难觅i
    2021-01-08 01:23

    Since the first count() is returning a hash, use whatever Ruby enumerable operations you wish on it. Example:

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

    See also:

    • http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000292
    • http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-count

提交回复
热议问题