Strict_loading function to suppress the occurrence of N + 1 problem added from rails 6.1

Since rails 6.1rc has been released, we have summarized the new strict_loading features. See: https://weblog.rubyonrails.org/releases/

  1. #strict_loading

By marking an object, the exception ActiveRecord :: StrictLoadingViolationError will be thrown when the object is loaded with an association delay.

$ dev = Developer.strict_loading.first
$ dev.audit_logs.to_a
=> ActiveRecord::StrictLoadingViolationError: Developer is marked as strict_loading and AuditLog cannot be lazily loaded.

See: https://github.com/rails/rails/pull/37400

  1. has_many :projects, strict_loading: true How to set in the association option. Exception ActiveRecord :: StrictLoadingViolationError is thrown when lazy loading is performed on all specified associations.
class Developer < ApplicationRecord
  has_many :projects, strict_loading: true
end

dev = Developer.first
dev.projects.first
# => ActiveRecord::StrictLoadingViolationError: The projects association is marked as strict_loading and cannot be lazily loaded.

See: https://github.com/rails/rails/pull/38541

  1. self.strict_loading_by_default = true How to specify in model units. All associations from the entire specified model will raise the exception ActiveRecord :: StrictLoadingViolationError when lazy loading.
class Developer < ApplicationRecord
  self.strict_loading_by_default = true

  has_many :projects
end

dev = Developer.first
dev.projects.first
# => ActiveRecord::StrictLoadingViolationError Exception: Developer is marked as strict_loading and Project cannot be lazily loaded.

See: https://github.com/rails/rails/pull/39491

Other

It is now possible to select log output as well as exception occurrence in the behavior when strict_loding is detected.

config.active_record.action_on_strict_loading_violation enables raising or logging an exception if strict_loading is set on an association. The default value is :raise in all environments. It can be changed to :log to send violations to the logger instead of raising.

config.active_record.action_on_strict_loading_violation = :log

See https://github.com/rails/rails/pull/40511 https://edgeguides.rubyonrails.org/configuring.html#configuring-active-record

This makes it possible to raise an exception in the development and test environments and keep the log output in the production environment.

Discussion at WTF

The community is also actively discussing ways to prevent N + 1 from occurring. https://discuss.rubyonrails.org/t/n-1-queries-should-be-the-exception-not-the-easy-trap-to-fall-in/74537/14

Recommended Posts

Strict_loading function to suppress the occurrence of N + 1 problem added from rails 6.1
Kaminari --Added pagination function of Rails
The process of introducing Vuetify to Rails
[Rails] Button to return to the top of the page
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Rails] Implementation procedure when public / private functions are added to the posting function
How to write Scala from the perspective of Java
The story of migrating from Paperclip to Active Storage
[Beginner] Discover the N + 1 problem! How to use Bullet
[Rails] How to change the column name of the table
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Rails] How to get the contents of strong parameters
[Ruby On Rails] How to search and save the data of the parent table from the child table
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
Get to the abbreviations from 5 examples of iterating Java lists
[Swift] I tried to implement the function of the vending machine
A note about the seed function of Ruby on Rails
How to get the longest information from Twitter as of 12/12/2016
[Rails] Function to search and list products from multi-level categories
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
[Rails] Where to be careful in the description of validation
Avoid the problem that session cannot be acquired when canary release when upgrading from Rails 4.2.x to 5.0.x
Don't underestimate the N + 1 problem!
[Rails] Implementation of category function
Explanation of the FizzBuzz problem
Rails ~ Understanding the message function ~
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to implement Ajax processing of like function in Rails
[Rails] How to get the URL of the transition source and redirect
A solution to the problem of blank spaces at the beginning of textarea
[Rails] Suppress unnecessary SQL by utilizing the cache control of the association
I tried to implement the image preview function with Rails / jQuery
Want to know what Ruby n is the power of 2? (Power judgment of 2)
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] Talk about paying attention to the return value of where
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
[Rails] How to put a crown mark on the ranking function
[Rails] Read the RSS of the site and return the contents to the front
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
(Ruby on Rails6) Create a function to edit the posted content
I tried to solve the problem of Google Tech Dev Guide
[With explanation of why to do it] How to prevent others from editing with Rails' CRUD app with authentication function
[Rails] Implementation procedure of the function to tag posts without gem + the function to narrow down and display posts by tags