Reducing N+1 Queries In Rails 7.1 With Strict Loading
Reduce N+1 queries in Rails v7.1 with `.strict_loading!`. Configure `config/application.rb` and `config/environments/development.rb` to log or raise violations. Chain `.strict_loading` in new controller code, especially actions.
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_stri...