diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index bc310cd13ee36317c6dc83aada6bfe89088de4bc..6fd5c466f66cc05d8ff3d4634ceb40f8fa855a7d 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -540,20 +540,52 @@ class CollectionProxy < Relation # has_many :pets # end # - # # This will execute: - # # SELECT COUNT(*) FROM "pets" WHERE "pets"."person_id" = ? [["person_id", 1]] # person.pets.size # => 3 + # # Executes: + # # + # # SELECT COUNT(*) + # # FROM "pets" + # # WHERE "pets"."person_id" = 1 # - # person.pets + # person.pets # This will execute a SELECT * FROM query # # => [ # # #, # # #, # # # # # ] # - # # Because the collection is already loaded, this will behave like - # collection.size and no SQL count query is executed. # person.pets.size # => 3 + # # Because the collection is already loaded, this will behave like + # # collection.size and no SQL count query is executed. + + ## + # :method: length + # + # Returns the size of the collection calling +size+ on the target. + # If the collection has been already loaded +length+ and +size+ are + # equivalent. If not and you are going to need the records anyway this + # method will take one less query because loads the collection. Otherwise + # +size+ is more efficient. + # + # class Person < ActiveRecord::Base + # has_many :pets + # end + # + # person.pets.length # => 3 + # # Executes: + # # + # # SELECT "pets".* + # #  FROM "pets" + # # WHERE "pets"."person_id" = 1 + # + # # Because the collection is loaded, you can + # # call the collection without execute a query: + # person.pets + # # => [ + # # #, + # # #, + # # # + # # ] ## # :method: empty?