public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Ensure AssociationCollection#size considers all unsaved record. [#305 
state:resolved] [sds]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
lifo (author)
Tue Jul 01 19:17:33 -0700 2008
commit  474d42538269a141687c7c66bef6575b4682b15d
tree    42aebc676eeb1f6e0035aa1595fb1bed0dea01ca
parent  7378e237342443addb1691795ac9457250b6db1e
...
187
188
189
190
 
191
192
193
...
187
188
189
 
190
191
192
193
0
@@ -187,7 +187,7 @@ module ActiveRecord
0
         if @owner.new_record? || (loaded? && !@reflection.options[:uniq])
0
           @target.size
0
         elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
0
-          unsaved_records = Array(@target.detect { |r| r.new_record? })
0
+          unsaved_records = @target.select { |r| r.new_record? }
0
           unsaved_records.size + count_records
0
         else
0
           count_records
...
384
385
386
 
 
 
 
 
 
 
387
388
389
...
384
385
386
387
388
389
390
391
392
393
394
395
396
0
@@ -384,6 +384,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal 2, company.clients_of_firm(true).size
0
   end
0
 
0
+  def test_collection_size_after_building
0
+    company = companies(:first_firm)  # company already has one client
0
+    company.clients_of_firm.build("name" => "Another Client")
0
+    company.clients_of_firm.build("name" => "Yet Another Client")
0
+    assert_equal 3, company.clients_of_firm.size
0
+  end
0
+
0
   def test_build_many
0
     company = companies(:first_firm)
0
     new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }

Comments