diff --git a/lib/will_paginate/active_record.rb b/lib/will_paginate/active_record.rb index b88395cb4..1b29e5062 100644 --- a/lib/will_paginate/active_record.rb +++ b/lib/will_paginate/active_record.rb @@ -73,7 +73,7 @@ def count # workaround for Active Record 3.0 def size - if !loaded? and limit_value + if !loaded? and limit_value and group_values.empty? [super, limit_value].min else super @@ -83,12 +83,9 @@ def size # overloaded to be pagination-aware def empty? if !loaded? and offset_value - rel_count = count - if rel_count.respond_to?(:size) and !rel_count.is_a?(Integer) - rel_count.size <= offset_value - else - rel_count <= offset_value - end + result = count + result = result.size if result.respond_to?(:size) and !result.is_a?(Integer) + result <= offset_value else super end diff --git a/spec/finders/active_record_spec.rb b/spec/finders/active_record_spec.rb index 2c85d37eb..aaa0e69c4 100644 --- a/spec/finders/active_record_spec.rb +++ b/spec/finders/active_record_spec.rb @@ -146,6 +146,13 @@ }.should run_queries(1) end + it "supports `size` for grouped queries" do + topics = Topic.group(:project_id).paginate :page => 1, :per_page => 3 + lambda { + topics.size.should == {nil=>2, 1=>2} + }.should run_queries(1) + end + it "overrides total_entries count with a fixed value" do lambda { topics = Topic.paginate :page => 1, :per_page => 3, :total_entries => 999