aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-08-05 08:57:58 -0700
committerJon Leighton <j@jonathanleighton.com>2012-08-05 08:57:58 -0700
commit6126f02cf903fa206b11d2dc2eeb5f197a29b965 (patch)
treea5051bc961784669e5c0247da6ef3b75959737ad /activerecord/lib/active_record/associations
parentdaf9f9ffa608b24c475403b9a0b3839ca373b2db (diff)
parentd1f7590577c61e33bcfa1b146971c47924823238 (diff)
downloadrails-6126f02cf903fa206b11d2dc2eeb5f197a29b965.tar.gz
rails-6126f02cf903fa206b11d2dc2eeb5f197a29b965.tar.bz2
rails-6126f02cf903fa206b11d2dc2eeb5f197a29b965.zip
Merge pull request #7270 from beerlington/use_exists_for_empty
Changing AR:CollectionAssociation#empty? to use #exists?
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb14
1 files changed, 11 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index f8c1103ea9..a84eda1d3b 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -270,12 +270,20 @@ module ActiveRecord
load_target.size
end
- # Returns true if the collection is empty. Equivalent to
- # <tt>collection.size.zero?</tt>. If the collection has not been already
+ # Returns true if the collection is empty.
+ #
+ # If the collection has been loaded or the <tt>:counter_sql</tt> option
+ # is provided, it is equivalent to <tt>collection.size.zero?</tt>. If the
+ # collection has not been loaded, it is equivalent to
+ # <tt>collection.exists?</tt>. If the collection has not already been
# loaded and you are going to fetch the records anyway it is better to
# check <tt>collection.length.zero?</tt>.
def empty?
- size.zero?
+ if loaded? || options[:counter_sql]
+ size.zero?
+ else
+ !scope.exists?
+ end
end
# Returns true if the collections is not empty.