Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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]
  • Loading branch information...
commit dc8bf7515de85f5bc28d17e96edf4a3e74a858da 1 parent 157141b
@miloops miloops authored NZKoz committed
View
2  activerecord/lib/active_record/associations/association_collection.rb
@@ -238,6 +238,8 @@ def create!(attrs = {})
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
View
2  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -249,7 +249,9 @@ def test_find_grouped
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

2 comments on commit dc8bf75

@fxn
Owner

The rdoc was untouched, but it gives a clear message that you are going to do a COUNT unless loaded. That’s no longer true.

@fxn
Owner

Sorry, the trailing dot was taken as part of the URL. Again :-) http://github.com/rails/rails/commit/4db7e8de1160de6c813a33266fa415849e25fba6

Please sign in to comment.
Something went wrong with that request. Please try again.