Allow reload
to be default_scoped
#40805
Merged
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.
reload
is notdefault_scoped
by default because you could becreating a record that does not match your default scope and therefore
reload
wouldn't find the record.However, in the case of sharding an application you may want
reload
tosupport
default_scope
because you'll always have the correct scopeset. To accomplish this I added a new method that checks if there are
any default scopes defined that are set to run for
all_queries
. Ifthere are then don't
unscope
the find methods.If there is a case where you do want the default scope to apply to all
queries but be able to turn that off, I've added a
unscoped
option toreload
. This would use the original behavior regardless of whether thedefault_scope
was used for all queries.Additionally, this exposes a new public method
default_scopes?
thatreturns true if there are default scopes. If
all_queries
is passed todefault_scopes?
the method will only return true if there is a defaultscope for the model that has
all_queries
set to true. If there are nodefault scopes with
all_queries
then it will return false.