Returns the number of records in this collection.
If the association has a counter cache it gets that value. Otherwise it will attempt to do a count via SQL, bounded to :limit if there’s one. Some configuration options like :group make it impossible to do an SQL count, in those cases the array count will be used.
That does not depend on whether the collection has already been loaded or not. The size method is the one that takes the loaded flag into account and delegates to count_records if needed.
If the collection is empty the target is set to an empty array and the loaded flag is set to true as well.
# File activerecord/lib/active_record/associations/has_many_association.rb, line 35 def count_records count = if has_cached_counter? owner.send(:read_attribute, cached_counter_attribute_name) elsif options[:counter_sql] || options[:finder_sql] reflection.klass.count_by_sql(custom_counter_sql) else scoped.count end # If there's nothing in the database and @target has no new records # we are certain the current target is an empty array. This is a # documented side-effect of the method that may avoid an extra SELECT. @target ||=  and loaded! if count == 0 [options[:limit], count].compact.min end