diff options
author | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-22 12:17:00 -0500 |
---|---|---|
committer | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-22 12:17:00 -0500 |
commit | f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3 (patch) | |
tree | 6a31844747f7a93002b144fff4a328f427442288 | |
parent | 3e847afbdd88d53bbf85eb2436ead2dab88f6e5b (diff) | |
download | rails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.tar.gz rails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.tar.bz2 rails-f539d98cc8838b0f7c3c31465cdc070ab9c1e9f3.zip |
add CollectionProxy#size documentation
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 27 |
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? |