Strange ActiveRecord behaviour when using merge and joins #12953

Open
iwiznia opened this Issue Nov 19, 2013 · 5 comments

Comments

Projects
None yet
5 participants
Contributor

iwiznia commented Nov 19, 2013

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.

Contributor

take commented Dec 3, 2013

Hi, I looked into the test code and,

but when you combine joins as strings and joins as symbols or classes, it mangles the order of the joins.

since the following test case passes(which you wrote), isn't the combination which fails is only joins as string, and joins as classes ?

  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
Contributor

iwiznia commented Dec 3, 2013

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

iwiznia added the stale label Apr 23, 2014

Owner

rafaelfranca commented May 1, 2014

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 4-1-stable, 4-0-stable branches 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 closed this May 27, 2014

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches 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.

@thedarkone thedarkone added a commit to thedarkone/rails that referenced this issue Jun 1, 2014

@thedarkone thedarkone Preserve user supplied JOIN order.
Fixes #12953.
1556744

@thedarkone thedarkone added a commit to thedarkone/rails that referenced this issue Jun 1, 2014

@thedarkone thedarkone Preserve user supplied JOIN order.
JOIN order is important, previous implementation always put
string or arel joins at then end (after auto-generated association
joins).

Fixes #12953.
c03d8c4

@thedarkone thedarkone added a commit to thedarkone/rails that referenced this issue Jun 2, 2014

@thedarkone thedarkone Preserve user supplied JOIN order.
JOIN clauses order is important, previous implementation always
put string or arel joins at then end (after auto-generated
association joins).

Fixes #12953.
f472523

@rafaelfranca rafaelfranca added attached PR and removed stale labels Jun 2, 2014

rafaelfranca reopened this Jun 2, 2014

@thedarkone thedarkone added a commit to thedarkone/rails that referenced this issue Jun 6, 2014

@thedarkone thedarkone Preserve user supplied JOIN order.
JOIN clauses order is important, previous implementation always
put string or arel joins at then end (after auto-generated
association joins).

Fixes #12953, #15488.
1657c5e

@thedarkone thedarkone added a commit to thedarkone/rails that referenced this issue Sep 12, 2014

@thedarkone thedarkone Preserve user supplied JOIN order.
JOIN clauses order is important, previous implementation always
put string or arel joins at then end (after auto-generated
association joins).

Fixes #12953, #15488, #16635.
2c80962

magikid commented Mar 13, 2016

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.

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