Permalink
Browse files

Fixed HABTM's CollectionAssociation size

HABTM should not include new records as part of #count_records as new records are already counted.

This addresses #14914.
  • Loading branch information...
1 parent 3d057a0 commit 9d4e2ab2cf01cef4127dea2e443688f809f3f785 @fredwu fredwu committed May 6, 2014
@@ -1,3 +1,12 @@
+* Fixed HABTM's CollectionAssociation size calculation.
+
+ HABTM should not include new records as part of #count_records as new
+ records are already counted.
+
+ Fixes #14914.
+
+ *Fred Wu*
+
* Keep track of dirty attributes after transaction is rollback.
Related #13166.
@@ -35,7 +35,7 @@ def insert_record(record, validate = true, raise = false)
private
def count_records
- load_target.size
+ load_target.reject { |r| r.new_record? }.size
end
def delete_records(records, method)
@@ -228,6 +228,14 @@ def test_habtm_unique_order_preserved
assert_equal developers(:poor_jamis, :jamis, :david), projects(:active_record).developers
end
+ def test_habtm_collection_size_from_build
+ devel = Developer.create("name" => "Fred Wu")
+ devel.projects << Project.create("name" => "Grimetime")
+ devel.projects.build
+
+ assert_equal 2, devel.projects.size
+ end
+
def test_build
devel = Developer.find(1)
proj = assert_no_queries { devel.projects.build("name" => "Projekt") }

0 comments on commit 9d4e2ab

Please sign in to comment.