6

Allow `reload` to be `default_scoped`

 3 years ago
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 therefore
reload 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 to
reload. This would use the original behavior regardless of whether the
default_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 to
default_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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK