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
And opt into raising in dev and test:
# in config/environments/development.rb
config.active_record.action_on_strict_loading_violation = :raise
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.
Top comments (0)