Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded, and by calling collection.size if it has. If the collection will likely have a size greater than zero, and if fetching the collection will be needed afterwards, one less SELECT query will be generated by using #length instead.
# File activerecord/lib/active_record/associations/has_many_through_association.rb, line 21 def size if has_cached_counter? owner._read_attribute cached_counter_attribute_name(reflection) elsif loaded? target.size else super end end