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
Order of "manual" joins messed up when using .merge #24281
Comments
Not sure what is the reason, but it seems by design Rails converts inner joins to outer joins when merging.
then these joins are called 'stashed' here
Can someone from Rails team give feedback about this? |
I have #15461 that attempts to fix this. The PR is a bit stale now, but I'll try to look into it really soon. |
@sgrif @thedarkone Any updates here? |
I will not be able to work on my PR until after 22nd. |
I think the "left_outer_joins" method also needs to be taken into consideration. Right now, it is treated as a distinct list of values from the joins_values, so it's impossible to keep the order. The merge of associations also doesn't treat the left_joins the same as the regular joins. |
Any updates on this one? |
Shameless plug: I made a gem to do conditions on associations without using You can read about all of the issues with the alternatives here: activerecord_where_assoc - ALTERNATIVES_PROBLEMS This is the gem: activerecord_where_assoc |
For those who has the same problem there is a workaround:
|
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.
When using a JOIN like
.joins('INNER JOIN posts ON users.id = posts.user_id')
instead of.joins(:posts)
, and then using.merge
with a relation with another JOIN defined on (in this case)posts
, the JOIN with that other table gets put in the front, which is not something that MySQL is happy about.This description is probably a bit confusing, but I hope the gist makes clear what I mean (see below).
Steps to reproduce
See https://gist.github.com/tbreier/e388c3f4727f985126a1
Expected behavior
.merge works when the scope to be merged with a) is joined manually (not via symbol describing association) and b) contains a join with another table
Actual behavior
The gist above crashes.
System configuration
ActiveRecord 4.2.6, mysql2 0.4.3, MySQL 5.6.27, Ruby 2.1.2
By the way, I am curious behind the rationale of converting joins contained in the scope to be merged to a
LEFT OUTER JOIN
. This appears to be intended, as described in #16140.The text was updated successfully, but these errors were encountered: