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

Apply scope to association subqueries #48487

Merged
merged 1 commit into from Jun 20, 2023

Conversation

lazaronixon
Copy link
Contributor

@lazaronixon lazaronixon commented Jun 15, 2023

Motivation / Background

Apply scope to association subqueries. (belongs_to/has_one/has_many).

Given:

class Author < ApplicationRecord
  has_many :welcome_posts, -> { where(title: "welcome") } , class_name: "Post"
end

Before:

Author.where(welcome_posts: Post.all)
#=> SELECT (...) WHERE "authors"."id" IN (SELECT "posts"."author_id" FROM "posts")

Later:

Author.where(welcome_posts: Post.all)
#=> SELECT (...) WHERE "authors"."id" IN (SELECT "posts"."author_id" FROM "posts" WHERE "posts"."title" = 'welcome')

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@lazaronixon lazaronixon force-pushed the scope-subqueries branch 5 times, most recently from 62cd9a0 to ee061f2 Compare June 16, 2023 18:33
@lazaronixon lazaronixon changed the title Apply scope to has_one/has_many association subqueries Apply scope to association subqueries Jun 16, 2023
@tenderlove tenderlove merged commit 6264c1d into rails:main Jun 20, 2023
9 checks passed
@rafaelfranca
Copy link
Member

This is a huge breaking change. Conditions that were not applied in previous versions of Rails will be applied now giving people different results.

@lazaronixon can you open a PR to make this behavior opt-in when an option is passed to the association? Like has_many :welcome_posts, -> { where(title: "welcome") } , class_name: "Post", apply_on_subqueries: true

@aharpole
Copy link
Contributor

aharpole commented Jun 29, 2023

Another thing this change breaks is that if you pass a block that takes a parameter in as a scope, trying to use the association in a subquery now results in an ArgumentError.

luanzeba#4 provides a failing test.

Making this behavior opt in provides us a workaround in the meantime.

rafaelfranca added a commit that referenced this pull request Jun 29, 2023
This reverts commit 6264c1d, reversing
changes made to 6c80bcd.

Reason: Still discussion about the feature. We want to make it opt-in
but we need to better understand why people would want to opt-in to
this behavior.
aharpole pushed a commit to aharpole/rails that referenced this pull request Jun 29, 2023
…ies"

This reverts commit 6264c1d, reversing
changes made to 6c80bcd.

Reason: Still discussion about the feature. We want to make it opt-in
but we need to better understand why people would want to opt-in to
this behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants