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 it’s more likely than not that the collection does have a size larger than zero, and you need to fetch that collection afterwards, it’ll take one fewer SELECT query if you use #length.
# File activerecord/lib/active_record/associations/has_many_through_association.rb, line 31 def size return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter? return @target.size if loaded? return count end