Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #14995 from fredwu/habtm_relation_size_calculation…

…_fix-4-0-stable

Habtm relation size calculation fix for 4.0 branch
  • Loading branch information...
commit bb136488e16665321b7bc474538cf058ed2995b9 2 parents 3d057a0 + 9d4e2ab
@carlosantoniodasilva carlosantoniodasilva authored
View
9 activerecord/CHANGELOG.md
@@ -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.
View
2  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -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)
View
8 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -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") }
Please sign in to comment.
Something went wrong with that request. Please try again.