No commit activity in last 3 years
No release in over 3 years
Efficient Rails group count if you need the summed up count instead of the hash. Can be used with Kaminari to speed up total_pages and total_count.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.1.0
 Project Readme

ActiveRecord Group Count¶ ↑

Installing¶ ↑

Add it to your project’s Gemfile

gem "active_record_group_count"

and run

bundle install

Description¶ ↑

When you do group in Rails (SQL GROUP BY), count will return an ActiveSupport::OrderedHash. Sometimes you want the sum count (count of all the results, not by group). You can do count.keys.count, however this has serious performance issues if you have a lot of groups in the results.

I wrote an ActiveRecord scope that will override the count method to perform a more optimised count query (of course it will now return a FixNum instead of a Hash). You can use this in combination with Kaminari and get much more efficient calculation of total_pages and total_count.

Use¶ ↑

Just use the returns_count_sum scope before calling count:

MyModel.group(:something).returns_count_sum.count

You can use it with Kaminari too (it’s much faster):

MyModel.group(:something).returns_count_sum.page(1).per(10).total_pages

Tests¶ ↑

I’ve tested this project using RSpec.

You can run the tests by running

rake spec

in the root directory of this gem (so you have to clone it first).

Problems¶ ↑

If you have problems open an issue here on Github.

License¶ ↑

This project rocks and uses MIT-LICENSE.