aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-11 13:21:17 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-11 13:21:17 +0000
commitfe8fb574c06156ca7eef7367c85bd12991948806 (patch)
tree3bf529a0798e2e6f8f08d4a009bf2ae732d5c57b
parent45ea2bd4528aab5394cb5c94528f5b2c001ed518 (diff)
downloadrails-fe8fb574c06156ca7eef7367c85bd12991948806.tar.gz
rails-fe8fb574c06156ca7eef7367c85bd12991948806.tar.bz2
rails-fe8fb574c06156ca7eef7367c85bd12991948806.zip
Added a differenciation between AssociationCollection#size and -length. Now AssociationCollection#size 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.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@392 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG5
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb11
2 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index ee9bc22384..1ca6349117 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Added a differenciation between AssociationCollection#size and -length. Now AssociationCollection#size 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.
+
* Added Base#attributes that returns a hash of all the attributes with their names as keys and clones of their objects as values #433 [atyp.de]
* Fixed that foreign keys named the same as the association would cause stack overflow #437 [Eric Anderson]
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)