When counting grouped records the target should be loaded to return a valid groups count result.

Without this change count_records will group for the count in the query and return erroneous results.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#937 state:committed]
This commit is contained in:
miloops 2008-09-15 13:23:50 -03:00 committed by Michael Koziarski
parent 157141b294
commit dc8bf7515d
2 changed files with 4 additions and 0 deletions

@ -238,6 +238,8 @@ def create!(attrs = {})
def size def size
if @owner.new_record? || (loaded? && !@reflection.options[:uniq]) if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
@target.size @target.size
elsif !loaded? && @reflection.options[:group]
load_target.size
elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
unsaved_records = @target.select { |r| r.new_record? } unsaved_records = @target.select { |r| r.new_record? }
unsaved_records.size + count_records unsaved_records.size + count_records

@ -249,7 +249,9 @@ def test_find_grouped
end end
def test_find_scoped_grouped def test_find_scoped_grouped
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
assert_equal 2, companies(:first_firm).clients_grouped_by_name.size
assert_equal 2, companies(:first_firm).clients_grouped_by_name.length assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
end end