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 a 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 30 def count_records count = if has_cached_counter? @owner.send(:read_attribute, cached_counter_attribute_name) elsif @reflection.options[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) else @reflection.klass.count(:conditions => @counter_sql, :include => @reflection.options[:include]) 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 if @reflection.options[:limit] count = [ @reflection.options[:limit], count ].min end return count end