Skip to content

Commit

Permalink
Fixed HABTM's CollectionAssociation size
Browse files Browse the repository at this point in the history
HABTM should fall back to using the normal CollectionAssociation's size calculation if the collection is not cached or loaded.

This addresses both #14913 and #14914 for master.
  • Loading branch information
fredwu committed May 8, 2014
1 parent d2061a2 commit f045663
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,12 @@
* Fixed HABTM's CollectionAssociation size calculation.

HABTM should fall back to using the normal CollectionAssociation's size
calculation if the collection is not cached or loaded.

Fixes #14913 and #14914.

*Fred Wu*

* Return a non zero status when running `rake db:migrate:status` and migration table does
not exist.

Expand Down
Expand Up @@ -23,7 +23,7 @@ def size
elsif loaded?
target.size
else
count
super
end
end

Expand Down
Expand Up @@ -217,6 +217,24 @@ 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_habtm_collection_size_from_params
devel = Developer.new({
projects_attributes: {
'0' => {}
}
})

assert_equal 1, devel.projects.size
end

def test_build
devel = Developer.find(1)
proj = assert_no_queries { devel.projects.build("name" => "Projekt") }
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/developer.rb
Expand Up @@ -13,6 +13,8 @@ def find_most_recent
end
end

accepts_nested_attributes_for :projects

has_and_belongs_to_many :projects_extended_by_name,
-> { extending(DeveloperProjectsAssociationExtension) },
:class_name => "Project",
Expand Down

0 comments on commit f045663

Please sign in to comment.