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
Fix override existing join types in the query in the where.associated
method
#51078
Fix override existing join types in the query in the where.associated
method
#51078
Conversation
0c0e16c
to
1c57bd5
Compare
Thanks for the PR @saleh-alhaddad. class Post < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :posts
end
author1 = Author.create! name: 'Nora'
post1 = Post.create! title: "First post", author: author1
post2 = Post.create! title: "Second post"
assert_equal Post.where.associated(:author).to_a, [post1]
assert_equal Post.where.missing(:author).to_a, [post2] |
1c57bd5
to
94e1304
Compare
@saleh-alhaddad It's clear you want to allow changing the join. It's not clear what would be the benefit. For example, your added test has the same results as not passing the rails/activerecord/test/cases/relation/where_chain_test.rb Lines 24 to 29 in 9e01d93
If this case is already supported, why do you need to specify the Post.associated(:author)
Post.joins(:author)
Post.left_joins(:author).where.not(author_id: nil) |
94e1304
to
7b32782
Compare
where.associated
method
57b2dbc
to
ec8bfa3
Compare
@p8 I updated the PR to fix an issue in the method, please read again the description and reply if something is wrong, thanks for you |
ec8bfa3
to
77360db
Compare
@saleh-alhaddad please don’t ping commiters. Most are volunteers and contribute in their spare time. Hopefully someone will find the time to look at this PR. |
activerecord/CHANGELOG.md
Outdated
@@ -3,6 +3,18 @@ | |||
|
|||
*Jason Nochlin* | |||
|
|||
* Fix an issue in the `where.associated` method only allows filtering associated records with an `inner join`, | |||
which can potentially override existing join types in the query. | |||
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. | |
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. |
* Fix an issue in the `where.associated` method only allows filtering associated records with an `inner join`, | ||
which can potentially override existing join types in the query. | ||
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. | ||
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries. | |
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries. |
which can potentially override existing join types in the query. | ||
The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. | ||
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries. | ||
Example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example: | |
Example: |
activerecord/CHANGELOG.md
Outdated
asoiciated will use left_joins alternative use deafult joins | ||
```ruby | ||
Post.left_joins(:author).where.associated(:author) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asoiciated will use left_joins alternative use deafult joins | |
```ruby | |
Post.left_joins(:author).where.associated(:author) | |
``` | |
```ruby | |
# associated will use LEFT JOIN for this query instead of using JOIN | |
Post.left_joins(:author).where.associated(:author) | |
``` |
@@ -72,10 +72,17 @@ def not(opts, *rest) | |||
# # INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" | |||
# # INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" | |||
# # WHERE "authors"."id" IS NOT NULL AND "comments"."id" IS NOT NULL | |||
# | |||
# Additionally, you can define joine type in the scope and associated will not use default that is joins |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Additionally, you can define joine type in the scope and associated will not use default that is joins | |
# Additionally, you can define join type in the scope and +associated+ will not use default that it is `JOIN`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to change the Additionally
in the topic above, probably removing it.
@@ -72,10 +72,17 @@ def not(opts, *rest) | |||
# # INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" | |||
# # INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" | |||
# # WHERE "authors"."id" IS NOT NULL AND "comments"."id" IS NOT NULL | |||
# | |||
# Additionally, you can define joine type in the scope and associated will not use default that is joins | |||
# Example: Post.left_joins(:author).where.associated(:author) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Example: Post.left_joins(:author).where.associated(:author) | |
# Post.left_joins(:author).where.associated(:author) | |
# # Put the SQL here |
77360db
to
d5545fd
Compare
@rafaelfranca thanks updated |
d5545fd
to
3400aac
Compare
👏 🚀 |
Motivation / Background
Currently, the
where.associated
method in Rails only allows filtering associated records with aninner join
, which can potentially override existing join types in the query. The fix ensures that the association is joined using the appropriate join type (either inner join or left outer join) based on the existing joins in the scope. This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries.Example:
Old query
after fixing the issue, the query:
New Query
This enhancement empowers developers with greater flexibility in querying associated data, facilitating scenarios such as:
Detail
The pivotal improvement in this Pull Request is the inclusion of a check ensuring that associations are joined using the appropriate join type (either inner join or left outer join) based on existing joins within the scope. This safeguards against unintentional overrides of existing join types and fosters consistency in the generated SQL queries.
Additional Information
This refinement is informed by common requirements articulated by Rails developers in various forums and discussions. Analogous functionality is prevalent in certain popular ORM extensions, underscoring its utility. The implementation is crafted to preserve clarity and coherence within the existing codebase.
Note: This is a draft description and should be adapted to your specific implementation details and the project's context.
FYI: After merging this PR, I will check the
missing
method and fix this issue in a separate PR alternative same PR.Performance:
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]