class ActiveRecord::Associations::CollectionProxy
def size
# Because the collection is already loaded, this will behave like
person.pets.size # => 3
# ]
# #
# #
# #
# => [
person.pets # This will execute a SELECT * FROM query
# executes something like SELECT COUNT(*) FROM "pets" WHERE "pets"."person_id" = 1
person.pets.size # => 3
end
has_many :pets
class Person < ActiveRecord::Base
+length+ will take one less query. Otherwise +size+ is more efficient.
equivalent. If not and you are going to need the records anyway
If the collection has been already loaded +size+ and +length+ are
it executes a SELECT COUNT(*) query. Else it calls collection.size.
Returns the size of the collection. If the collection hasn't been loaded,
def size @association.size end