Stop Using .any? the Wrong Way in Rails

📰 Dev.to · Pavel Myslik

Learn to avoid common pitfalls when using .any? in Rails to prevent silent loading of thousands of records into memory

intermediate Published 24 Feb 2026
Action Steps
  1. Use .exists? instead of .any? when checking for the presence of records
  2. Avoid passing a block to .any? to prevent loading records into memory
  3. Test your code with large datasets to identify potential performance issues
  4. Configure your Rails application to use eager loading or pagination to reduce memory usage
  5. Apply optimization techniques to your queries to reduce the amount of data loaded into memory
Who Needs to Know This

Rails developers and engineers can benefit from this knowledge to optimize their code and prevent potential performance issues

Key Insight

💡 Using .any? with a block can silently load thousands of records into memory, causing performance issues

Share This
🚨 Avoid using .any? the wrong way in Rails to prevent silent loading of thousands of records into memory! 🚨

Key Takeaways

Learn to avoid common pitfalls when using .any? in Rails to prevent silent loading of thousands of records into memory

Full Article

A single block passed to .any? can silently load thousands of records into memory. No warnings. No...
Read full article → ← Back to Reads