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

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up