DEV Community

Augusts Bautra
Augusts Bautra

Posted on

Adopt .strict_loading gradually

Everyone knows that in Rails projects N+1 queries are a common performance problem. This stems from Rails' developer-friendly lazy-loading behavior, it's very easy to take on technical debt without even realizing it!

Thankfully, with v7.1 there's a way to configure you Rails project in such a way as to significantly reduce N+1 queries - .strict_loading! See Dinesh's excellent post about details.

I recommend starting by setting any violations to only log - you don't want existing problems and future slip-ups to raise in production:

# in config/application.rb
config.active_record.action_on_strict_loading_violation = :log
Enter fullscreen mode Exit fullscreen mode

And opt into raising in dev and test:

# in config/environments/development.rb
config.active_record.action_on_strict_loading_violation = :raise
Enter fullscreen mode Exit fullscreen mode

Now require all new controller code, especially any collections loaded in #index actions to chain .strict_loading. This can be left for code review, or you can make the requirement be more strict with a custom cop.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video