aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-22 12:17:00 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-22 12:17:00 -0500
commitf539d98cc8838b0f7c3c31465cdc070ab9c1e9f3 (patch)
tree6a31844747f7a93002b144fff4a328f427442288 /activerecord/lib/active_record
parent3e847afbdd88d53bbf85eb2436ead2dab88f6e5b (diff)
downloadrails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.tar.gz
rails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.tar.bz2
rails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.zip
add CollectionProxy#size documentation
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb27
1 files changed, 25 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index 776d65294e..bc310cd13e 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -277,7 +277,6 @@ module ActiveRecord
# person.pets.create!(name: nil)
# # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
-
##
# :method: concat
#
@@ -337,7 +336,6 @@ module ActiveRecord
#
# person.pets.replace(["doo", "ggie", "gaga"])
# # => ActiveRecord::AssociationTypeMismatch: Pet expected, got String
-
##
# :method: delete_all
@@ -531,6 +529,31 @@ module ActiveRecord
# person.pets # => []
#
# Pet.find(4, 5, 6) # => ActiveRecord::RecordNotFound: Couldn't find all Pets with IDs (4, 5, 6)
+
+ ##
+ # :method: size
+ #
+ # Returns the size of the collection. If the collection hasn't been loaded,
+ # it executes a <tt>SELECT COUNT(*)</tt> query.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # # This will execute:
+ # # SELECT COUNT(*) FROM "pets" WHERE "pets"."person_id" = ? [["person_id", 1]]
+ # person.pets.size # => 3
+ #
+ # person.pets
+ # # => [
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
+ # # ]
+ #
+ # # Because the collection is already loaded, this will behave like
+ # <tt>collection.size</tt> and no SQL count query is executed.
+ # person.pets.size # => 3
##
# :method: empty?