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
Strange ActiveRecord behaviour when using merge and joins #12953
Comments
Hi, I looked into the test code and,
since the following test case passes(which you wrote), isn't the combination which fails is only def test_joins_on_correct_order_works
assert_equal Post.joins(:comments).merge(Comment.fails).to_sql, 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "other_table"'
end |
I don't understand your question...There's only one failing test case, the others are there to help pinpoint the problem, since they should be doing outputing the same as the failing test |
This issue has been automatically marked as stale because it has not been commented on for at least 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 Thank you for all your contributions. |
This issue has been automatically closed because of inactivity. If you can still reproduce this error on the Thank you for all your contributions. |
JOIN clauses order is important, previous implementation always put string or arel joins at then end (after auto-generated association joins). Fixes rails#12953, rails#15488.
JOIN clauses order is important, previous implementation always put string or arel joins at then end (after auto-generated association joins). Fixes rails#12953, rails#15488, rails#16635.
I'm not sure what versions @iwiznia was using but when I run this using ruby 2.3 and rails 4.2.6, all of the tests pass. |
We're running into this problem ourselves. Curious to see if there's any new thoughts on it. |
Currently, string joins are always applied as last joins part, and Arel join nodes are always applied as leading joins part (since rails#36304), it makes people struggled to preserve user supplied joins order. To mitigate this problem, preserve the order of string joins and Arel join nodes either before or after of association joins. Fixes rails#36761. Fixes rails#34328. Fixes rails#24281. Fixes rails#12953.
There's a strange behaviour on AR in which using merge on a scope varies the order of the joins depending on how you define the join (string vs symbol).
Here's the testcase:
https://gist.github.com/iwiznia/7547713
I know that in the test there's a table that doesn't exist, it's there only to check the order of the join clauses.
There are 3 tests which should yield the same result, but when you combine joins as strings and joins as symbols or classes, it mangles the order of the joins.
Maybe it has to do with the fact that it's using the join_sources of the Arel::SelectManager class, but I thought that was a valid use case, and doing the same thing, but calling to_sql first solves the error.
What do you think? Is this a bug? I think it is since the three join statements should produce the same sql.
The text was updated successfully, but these errors were encountered: