class ActiveRecord::Associations::HasManyAssociation

def count_records

the loaded flag is set to true as well.
If the collection is empty the target is set to an empty array and

account and delegates to +count_records+ if needed.
or not. The +size+ method is the one that takes the loaded flag into
That does not depend on whether the collection has already been loaded

to do an SQL count, in those cases the array count will be used.
there's one. Some configuration options like :group make it impossible
it will attempt to do a count via SQL, bounded to :limit if
If the association has a counter cache it gets that value. Otherwise

Returns the number of records in this collection.
def count_records
  count = if reflection.has_active_cached_counter?
    owner.read_attribute(reflection.counter_cache_column).to_i
  else
    scope.count(:all)
  end
  # If there's nothing in the database, @target should only contain new
  # records or be an empty array. This is a documented side-effect of
  # the method that may avoid an extra SELECT.
  if count == 0
    target.select!(&:new_record?)
    loaded!
  end
  [association_scope.limit_value, count].compact.min
end