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

Association loading isn't to be affected by scoping consistently #35868

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Apr 5, 2019

Follow-up of 5c71000, #29834, and #30271.

Currently, preloading and eager loading are not to be affected by
scoping, with the exception of unscoped.

But non eager loaded association access is still affected by scoping.

Although this is a breaking change, the association loading will work
consistently whether preloaded / eager loaded or not.

Before:

Post.where("1=0").scoping do
  Comment.find(1).post                   # => nil
  Comment.preload(:post).find(1).post    # => #<Post id: 1, ...>
  Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
end

After:

Post.where("1=0").scoping do
  Comment.find(1).post                   # => #<Post id: 1, ...>
  Comment.preload(:post).find(1).post    # => #<Post id: 1, ...>
  Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
end

Fixes #34638.
Fixes #35398.

Follow-up of 5c71000, rails#29834, and rails#30271.

Currently, preloading and eager loading are not to be affected by
scoping, with the exception of `unscoped`.

But non eager loaded association access is still affected by scoping.

Although this is a breaking change, the association loading will work
consistently whether preloaded / eager loaded or not.

Before:

```ruby
Post.where("1=0").scoping do
  Comment.find(1).post                   # => nil
  Comment.preload(:post).find(1).post    # => #<Post id: 1, ...>
  Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
end
```

After:

```ruby
Post.where("1=0").scoping do
  Comment.find(1).post                   # => #<Post id: 1, ...>
  Comment.preload(:post).find(1).post    # => #<Post id: 1, ...>
  Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
end
```

Fixes rails#34638.
Fixes rails#35398.
Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@eileencodes eileencodes added this to the 6.0.0 milestone Apr 5, 2019
@kamipo kamipo merged commit e9d1d76 into rails:master Apr 5, 2019
@kamipo kamipo deleted the association_isnt_to_be_affected_by_scoping_consistently branch April 5, 2019 19:57
kamipo added a commit that referenced this pull request Apr 6, 2019
kamipo added a commit to kamipo/rails that referenced this pull request Oct 19, 2019
The intent of rails#35868 is to make the association loading consistently, it
should not have any side-effect for new instance creation on association
relation.

Fixes rails#37138.
@norydev
Copy link

norydev commented Oct 21, 2019

Thank you @kamipo 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants