public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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]
miloops (author)
Mon Sep 15 09:23:50 -0700 2008
NZKoz (committer)
Mon Sep 15 09:32:05 -0700 2008
commit  dc8bf7515de85f5bc28d17e96edf4a3e74a858da
tree    3bcc577d8a233d6392b1b78c2b14afe928c2e514
parent  157141b2949b845e372ee703bfd6fba3ffb00415
...
238
239
240
 
 
241
242
243
...
238
239
240
241
242
243
244
245
0
@@ -238,6 +238,8 @@ module ActiveRecord
0
       def size
0
         if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
0
           @target.size
0
+        elsif !loaded? && @reflection.options[:group]
0
+          load_target.size
0
         elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
0
           unsaved_records = @target.select { |r| r.new_record? }
0
           unsaved_records.size + count_records
...
249
250
251
 
252
 
253
254
255
...
249
250
251
252
253
254
255
256
257
0
@@ -249,7 +249,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
   end
0
 
0
   def test_find_scoped_grouped
0
+    assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
0
     assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
0
+    assert_equal 2, companies(:first_firm).clients_grouped_by_name.size
0
     assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
0
   end
0
 

Comments

fxn Tue Sep 16 01:19:57 -0700 2008

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 Tue Sep 16 10:57:55 -0700 2008

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