Skip to content

[Fix #40109] Incorrect joins for polymorphic has_many through: with association scope #51507

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

Conversation

joshuay03
Copy link
Contributor

@joshuay03 joshuay03 commented Apr 6, 2024

Motivation / Background

Fixes #40109

Detail

Updates ActiveRecord::Reflection::PolymorphicReflection#join_scopes to not apply the previous reflection's join scopes if it's a through reflection, as the scope will eventually be built in the context of the polymorphic reflection.

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes 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.

@joshuay03 joshuay03 changed the title [Fix #40109] Fix invalid joins for polymorphic has_many through: wi… [Fix #40109] Fix invalid joins for polymorphic has_many through: with association scope Apr 6, 2024
@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch from 67f1828 to 696728a Compare April 6, 2024 03:11
@joshuay03 joshuay03 changed the title [Fix #40109] Fix invalid joins for polymorphic has_many through: with association scope [Fix #40109] Incorrect joins for polymorphic has_many through: with association scope Apr 6, 2024
author = authors(:bob)
Essay.create!(writer: author, category: category)

assert_equal 1, Post.joins(:authors_of_essays_named_bob).count
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before patch:

Screenshot 2024-04-06 at 1 14 02 PM

Copy link
Contributor Author

@joshuay03 joshuay03 Apr 6, 2024

Choose a reason for hiding this comment

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

SQL comparison

Before:

SELECT posts.*
FROM posts
INNER JOIN categories_posts
    ON categories_posts.post_id = posts.id
INNER JOIN categories
    ON categories.id = categories_posts.category_id
INNER JOIN essays
    ON essays.name = 'Bob'
    AND essays.writer_type = 'Author'
    AND essays.category_id = categories.name
INNER JOIN authors
    ON authors.name = 'Bob'
    AND authors.name = essays.writer_id

After:

SELECT posts.*
FROM posts
INNER JOIN categories_posts
    ON categories_posts.post_id = posts.id
INNER JOIN categories
    ON categories.id = categories_posts.category_id
INNER JOIN essays
    ON essays.writer_type = 'Author'
    AND essays.category_id = categories.name
INNER JOIN authors
    ON authors.name = 'Bob'
    AND authors.name = essays.writer_id

@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch from 696728a to 59a6dd2 Compare April 6, 2024 07:13
@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch from 59a6dd2 to 3cef40d Compare May 22, 2024 23:06
@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch 4 times, most recently from e1f122d to 560fa8e Compare July 9, 2024 18:16
@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch 2 times, most recently from 9894d77 to 563c299 Compare August 17, 2024 10:05
@MatheusRich MatheusRich added the ready PRs ready to merge label Aug 17, 2024
@joshuay03 joshuay03 force-pushed the fix-polymorphic-has-many-through-with-association-scope branch from 563c299 to 0d012fa Compare October 4, 2024 11:52
@kamipo kamipo merged commit 967c33d into rails:main Oct 7, 2024
3 checks passed
kamipo added a commit that referenced this pull request Oct 7, 2024
…ough-with-association-scope

[Fix #40109] Incorrect joins for polymorphic `has_many through:` with association scope
@joshuay03 joshuay03 deleted the fix-polymorphic-has-many-through-with-association-scope branch October 7, 2024 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activerecord ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

has_many through with scope block, STI and polymorphic associations that worked in 5.0 breaks in 6.0+
3 participants