Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`CollectionAssociation#empty?` respects newly builded records

  • Loading branch information...
commit ec07735641e4df13b644a85f1f300cf20134995a 1 parent 6581d79
Yves Senn senny authored
11 activerecord/CHANGELOG.md
View
@@ -1,5 +1,16 @@
## Rails 4.0.0 (unreleased) ##
+* Collection associations `#empty?` always respects builded records.
+ Fix #8879.
+
+ Example:
+
+ widget = Widget.new
+ widget.things.build
+ widget.things.empty? # => false
+
+ *Yves Senn*
+
* Remove support for parsing YAML parameters from request.
*Aaron Patterson*
2  activerecord/lib/active_record/associations/collection_association.rb
View
@@ -273,7 +273,7 @@ def empty?
if loaded? || options[:counter_sql]
size.zero?
else
- !scope.exists?
+ @target.blank? && !scope.exists?
end
end
7 activerecord/test/cases/associations/has_many_associations_test.rb
View
@@ -625,6 +625,13 @@ def test_collection_size_after_building
assert_equal 3, company.clients_of_firm.size
end
+ def test_collection_not_empty_after_building
+ company = companies(:first_firm)
+ assert_predicate company.contracts, :empty?
+ company.contracts.build
+ assert_not_predicate company.contracts, :empty?
+ end
+
def test_collection_size_twice_for_regressions
post = posts(:thinking)
assert_equal 0, post.readers.size
Please sign in to comment.
Something went wrong with that request. Please try again.