![](/style/images/good.png)
![](/style/images/bad.png)
Allow `reload` to be `default_scoped`
source link: https://github.com/rails/rails/pull/40805
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Member
eileencodes commented 10 days ago
reload
is not default_scoped
by default because you could be
creating a record that does not match your default scope and thereforereload
wouldn't find the record.
However, in the case of sharding an application you may want reload
to
support default_scope
because you'll always have the correct scope
set. 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
. If
there 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?
that
returns true if there are default scopes. If all_queries
is passed todefault_scopes?
the method will only return true if there is a default
scope for the model that has all_queries
set to true. If there are no
default scopes with all_queries
then it will return false.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK