diff --git a/lib/geared_pagination/recordset.rb b/lib/geared_pagination/recordset.rb index 2981a96..96262d3 100644 --- a/lib/geared_pagination/recordset.rb +++ b/lib/geared_pagination/recordset.rb @@ -32,7 +32,13 @@ def page_count end def records_count - @records_count ||= records.unscope(:limit).unscope(:offset).unscope(:select).count + if @records_count.nil? + @records_count = records.unscope(:limit).unscope(:offset).unscope(:select).size + # records count could be a hash if query has a group by clause -> in this case records count is number of groups + @records_count = @records_count.size if @records_count.is_a?(Hash) + end + + @records_count end private diff --git a/test/recordset_test.rb b/test/recordset_test.rb index 4dc66e4..8b93b40 100644 --- a/test/recordset_test.rb +++ b/test/recordset_test.rb @@ -76,4 +76,10 @@ class GearedPagination::RecordsetTest < ActiveSupport::TestCase recordset = GearedPagination::Recordset.new(select_scoped_records, per_page: [ 10, 15, 20 ]) assert_equal Recording.all.count, recordset.records_count end + + test "records count for group by" do + select_scoped_records = Recording.all.select(:id, :number).group(:id) + recordset = GearedPagination::Recordset.new(select_scoped_records, per_page: [ 10, 15, 20 ]) + assert_equal Recording.all.count, recordset.records_count + end end