aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 6283f3dc28..ca2adaa9c9 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -70,12 +70,21 @@ module ActiveRecord
@collection = []
end
+ # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and
+ # calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero
+ # and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length.
def size
if loaded? then @collection.size else count_records end
end
+ # Returns the size of the collection by loading it and calling size on the array. If you want to use this method to check
+ # whether the collection is empty, use collection.length.zero? instead of collection.empty?
+ def length
+ load_collection.size
+ end
+
def empty?
- size == 0
+ size.zero?
end
def uniq(collection = self)