Rails: Testing the number of database queries

There are a few tools to combat the dreaded n+1 queries. The bullet gem notifies you of missing eager-loading, and also if there is too much eager-loading. strict_loading in Rails 6.1+ forces developers to explicitly load associations on individual records, for a single association, for an entire model, or globally for all models.

But you can also actually write specs on how many database queries you expect. Use the make_database_queries RSpec matcher from the linked Ruby Gem.

Dominik Schöler 4 months ago