aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormiloops <miloops@gmail.com>2008-09-15 13:23:50 -0300
committerMichael Koziarski <michael@koziarski.com>2008-09-15 18:32:05 +0200
commitdc8bf7515de85f5bc28d17e96edf4a3e74a858da (patch)
tree3bcc577d8a233d6392b1b78c2b14afe928c2e514
parent157141b2949b845e372ee703bfd6fba3ffb00415 (diff)
downloadrails-dc8bf7515de85f5bc28d17e96edf4a3e74a858da.tar.gz
rails-dc8bf7515de85f5bc28d17e96edf4a3e74a858da.tar.bz2
rails-dc8bf7515de85f5bc28d17e96edf4a3e74a858da.zip
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]
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb2
2 files changed, 4 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 8de528f638..afb817f8ae 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -238,6 +238,8 @@ module ActiveRecord
def size
if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
@target.size
+ elsif !loaded? && @reflection.options[:group]
+ load_target.size
elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
unsaved_records = @target.select { |r| r.new_record? }
unsaved_records.size + count_records
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 5bcbc5eb5b..ba750b266c 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -249,7 +249,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
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 2, companies(:first_firm).clients_grouped_by_name.size
assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
end