activerecord/lib/active_record/associations/collection_association.rb
# Returns the size of the collection by executing a SELECT COUNT(*)
# query if the collection hasn't been loaded, and calling
# <tt>collection.size</tt> if it has.
#
# If the collection has been already loaded +size+ and +length+ are
# equivalent. If not and you are going to need the records anyway
# +length+ will take one less query. Otherwise +size+ is more efficient.
#
# This method is abstract in the sense that it relies on
# +count_records+, which is a method descendants have to provide.
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 the collection has been already loaded +size+ and +length+ are
equivalent. If not and you are going to need the records anyway
+length+ will take one less query. Otherwise +size+ is more efficient.

This method is abstract in the sense that it relies on
+count_records+, which is a method descendants have to provide.
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 the collection has been already loaded + size + and + length + are equivalent . If not and you are going to need the records anyway + length + will take one less query . Otherwise + size + is more efficient . This method is abstract in the sense that it relies on + count_records + which is a method descendants have to provide .
def size
        if !find_target? || loaded?
          if association_scope.distinct_value
            target.uniq.size
          else
            target.size
          end
        elsif !loaded? && !association_scope.group_values.empty?
          load_target.size
        elsif !loaded? && !association_scope.distinct_value && target.is_a?(Array)
          unsaved_records = target.select(&:new_record?)
          unsaved_records.size + count_records
        else
          count_records
        end
      end
def size if ! find_target? || loaded? if association_scope . distinct_value target . uniq . size else target . size end elsif ! loaded? && ! association_scope . group_values . empty? load_target . size elsif ! loaded? && ! association_scope . distinct_value && target . is_a? ( Array ) unsaved_records = target . select ( & :new_record? ) unsaved_records . size + count_records else count_records end end
Returns the size of the collection by executing a SELECT COUNT(*)
query if the collection hasn't been loaded, and calling
<tt>collection.size</tt> if it has.
Returns the size of the collection by executing a SELECT COUNT ( * ) query if the collection hasn ' t been loaded and calling <tt > collection . size< / tt > if it has .