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

Merge with joins adds secondary joins to wrong part of query #32363

Closed
quickdudley opened this issue Mar 28, 2018 · 7 comments
Closed

Merge with joins adds secondary joins to wrong part of query #32363

quickdudley opened this issue Mar 28, 2018 · 7 comments

Comments

@quickdudley
Copy link

Steps to reproduce

I have a query along these lines:

joins("RIGHT JOIN (p LEFT JOIN s ON s.p_id = p.id) ON s.a_id = a.id OR p.a_id = a.id").merge(p_scope)

If p_scope contains no joins: the query works as expected. If p_scope contains joins: the full query is constructed incorrectly.

Expected behavior

In the event that p_scope contains joins: the generated SQL should be along the line of:

RIGHT JOIN (p LEFT JOIN s ON s.p_id = p.id) ON s.a_id = a.id OR p.a_id = a.id <join clauses from p_scope>

Actual behavior

The generated SQL is along the lines of:

<join clauses from p_scope> RIGHT JOIN (p LEFT JOIN s ON s.p_id = p.id) ON s.a_id = a.id OR p.a_id = a.id

This is a problem because SQL join syntax is left-associative: the join clauses from p_scope refer to columns which are outside of their namespace.

System configuration

Rails version:
Rails 5.0.0.1
Ruby version:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin17]

@y-yagi
Copy link
Member

y-yagi commented Mar 28, 2018

Can you create an executable test script using one of the templates here, that would demonstrate the problem you are seeing?

@kamipo
Copy link
Member

kamipo commented Mar 28, 2018

This is a same issue with #12953 and #24281.
A string joins doesn't work with symbol joins for now.

@quickdudley
Copy link
Author

I've also been seeing the same issue with symbol joins. During the weekend I will make a test script.

@quickdudley
Copy link
Author

Replicating the problem with symbol joins in a test script proved more difficult than expected. I currently have a scope which works as intended in rails console and in a test script but fails when called with the same arguments from a controller.

@rails-bot rails-bot bot added the stale label Jul 1, 2018
@rails-bot
Copy link

rails-bot bot commented Jul 1, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-2-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot closed this as completed Jul 9, 2018
@tibbon
Copy link

tibbon commented Jul 23, 2018

Thanks for working on this @quickdudley!

@rails-bot rails-bot bot removed the stale label Jul 23, 2018
@chumakoff
Copy link
Contributor

For those who has the same problem there is a workaround:

Instead of

BaseModel.joins("<base_scope_join_clauses>").merge(second_scope)

do this:

base_scope = BaseModel.joins("<base_scope_join_clauses>")
BaseModel.joins(base_scope.join_sources).joins(second_scope.join_sources)

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

No branches or pull requests

5 participants