Add n_plus_one_only mode to Core#strict_loading!#41704
Merged
rafaelfranca merged 1 commit intorails:mainfrom Mar 25, 2021
Merged
Add n_plus_one_only mode to Core#strict_loading!#41704rafaelfranca merged 1 commit intorails:mainfrom
rafaelfranca merged 1 commit intorails:mainfrom
Conversation
6aec1f5 to
2c59901
Compare
eileencodes
reviewed
Mar 20, 2021
Member
eileencodes
left a comment
There was a problem hiding this comment.
Can you add a changelog and add the nodoc I commented on?
3cadfb2 to
193db9c
Compare
rafaelfranca
approved these changes
Mar 22, 2021
Member
rafaelfranca
left a comment
There was a problem hiding this comment.
You need to fix the conflicts in the CHANGELOG
abhaynikam
reviewed
Mar 23, 2021
abhaynikam
reviewed
Mar 23, 2021
abhaynikam
reviewed
Mar 23, 2021
193db9c to
eeace1b
Compare
Add an optional mode argument to
Core#strict_loading! to support n_plus_one_only
mode. Currently, when we turn on strict_loading
for a single record, it will raise even if we are
loading an association that is relatively safe to
lazy load like a belongs_to. This can be helpful
for some use cases, but prevents us from using
strict_loading to identify only problematic
instances of lazy loading.
The n_plus_one_only argument allows us to turn
strict_loading on for a single record, and only
raise when a N+1 query is likely to be executed.
When loading associations on a single record,
this only happens when we go through a has_many
association type. Note that the has_many
association itself is not problematic as it only
requires one query. We do this by turning
strict_loading on for each record that is loaded
through the has_many. This ensures that any
subsequent lazy loads on these records will raise
a StrictLoadingViolationError.
For example, where a developer belongs_to a ship
and each ship has_many parts, we expect the
following behaviour:
developer.strict_loading!(mode: :n_plus_one_only)
# Do not raise when a belongs_to association
# (:ship) loads its has_many association (:parts)
assert_nothing_raised do
developer.ship.parts.to_a
end
refute developer.ship.strict_loading?
assert developer.ship.parts.all?(&:strict_loading?)
assert_raises ActiveRecord::StrictLoadingViolationError do
developer.ship.parts.first.trinkets.to_a
end
751ba32 to
5ec1cac
Compare
abhaynikam
added a commit
to abhaynikam/rails
that referenced
this pull request
Apr 15, 2021
The return type was changed in the PR rails#41704 after addition of mode option. The current documentation is misleading since documentation puropose strict_loading! would return boolean whereas it returns the current mode set. I can across this issue while debugging issue: rails#41827 and thought this should be brought to the attention. PR fixes the issue and would always return boolean based on strict_loading is enabled or disabled. ``` user.strict_loading! # => true user.strict_loading!(false) # => false user.strict_loading!(mode: :n_plus_one_only) # => true ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add an optional mode argument to
Core#strict_loading! to support n_plus_one_only
mode. Currently, when we turn on strict_loading
for a single record, it will raise even if we are
loading an association that is relatively safe to
lazy load like a belongs_to. This can be helpful
for some use cases, but prevents us from using
strict_loading to identify only problematic
instances of lazy loading.
The n_plus_one_only argument allows us to turn
strict_loading on for a single record, and only
raise when a N+1 query is likely to be executed.
When loading associations on a single record,
this only happens when we go through a has_many
association type. Note that the has_many
association itself is not problematic as it only
requires one query. We do this by turning
strict_loading on for each record that is loaded
through the has_many. This ensures that any
subsequent lazy loads on these records will raise
a StrictLoadingViolationError.
For example, where a developer belongs_to a ship
and each ship has_many parts, we expect the
following behaviour:
cc @eileencodes @tenderlove