Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

The versatility of the `exists?` method

My normal use of the exists? method in ActiveRecord has always been to toss it on the end of a query of some kind:

Animal.where(name: "dragon").exists?
# => false

However, I discovered that the method can be called directly on a class and passed options:

Animal.exists?(name: "dragon")
Animal.exists?(123)
Animal.exists?(id: [123, 345])

Check out the full documentation for more examples

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.