Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable scoping to apply to all queries #41397

Merged

Commits on Feb 11, 2021

  1. Enable scoping to apply to all queries

    Similar to rails#40720
    and rails#40805 this change allows for the
    `scoping` method to apply to all queries in the block. Previously this
    would only apply to queries on the class and not the instance. Ie
    `Post.create`, Post.all`, but not `post.update`, or `post.delete`.
    
    The change here will create a global scope that is applied to all
    queries for a relation for the duration of the block.
    
    Benefits:
    
    This change allows applications to add a scope to any query for the
    duration of a block. This is useful for applications using sharding to
    be able to control the query without requiring a `default_scope`. This
    is useful if you want to have more control over when a `scoping` is used
    on a relation. This also brings `scoping` in parity with the behavior of
    `default_scope` so there are less surprises between the behavior of
    these two methods.
    
    There are a caveats to this behavior:
    
    1) The `scoping` only applies to objects of the same type. IE you cannot
    scope `Post.where(blog_id: 1).scoping` and then expect `post.comments`
    will apply `blog_id = 1` to the `Comment` query. This is not possible
    because the scope is `posts.blog_id = 1` and we can't apply the `posts`
    scope to a `comments` query. To solve this, scopes must be nested.
    2) If a block is scoped to `all_queries` it cannot be unscoped without
    exiting the block. I couldn't find a way around this but ActiveRecord
    scoping is a bit complex and turning off `all_queries` when it's already
    on in nested scoping blocks had interesting behavior that I decided was
    best left out.
    eileencodes committed Feb 11, 2021
    Configuration menu
    Copy the full SHA
    d6e12ff View commit details
    Browse the repository at this point in the history