Today I Learned

hashrocket A Hashrocket project

Use .many? to Determine if Collection has > 1 recs

Today I learned about many?, which is a convenience method in ActiveSupport for determining if there is more than one record present. It's similar to any?, but the key difference is many? is strictly greater than, whereas any? is greater than or equal to.

  • any? returns true if >= 1 record
  • many? returns true if > 1 record.

many? is available on ActiveRecord::Relation and Enumerable (via ActiveSupport). When used on Enumerable it can be passed a block (similar to any?).

people.many? { |p| p.age > 26 }

Docs

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.