Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Merging JOINs when chaining scopes requires same case in JOIN expression

#3309

Affects: 2.3.4

Symptoms:
When chaining two scopes which contain the exact same JOIN expression (same tables, same join columns), then the merge fails if the SQL keywords used in the join expression are not of the same case.

Steps to reproduce:
Model.scoped(:joins => "JOIN x on a.id = b.id").scoped(:joins => "JOIN x ON a.id = b.id").scope(:find)
(note the different character case of the ON keyword)

Expected output:
=> {:readonly=>true, :joins=>["JOIN x on a.id = b.id"]}

Actual output:
=> {:readonly=>true, :joins=>["JOIN x ON a.id = b.id", "JOIN x on a.id = b.id"]}

Solution:
do a case insensitive comparison on join expressions when merging scopes.

Reported by Matthias · October 1st, 2009 @ 11:09 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Tobi
    Tobi
    • Tag changed from 2.3.4, :joins, activerecord, named_scope to 2.3.4, :joins, activerecord, named_scope, patch
    • Title changed from Merging JOINs when chaining scopes requires same case in JOIN expression to [PATCH] Merging JOINs when chaining scopes requires same case in JOIN expression

    Fixed with the patch attached. Pls check solution and test...

    Comparison algorithm may slow down the process, see for different approaches (including mine)
    http://stackoverflow.com/questions/1103327/how-to-uniq-an-array-cas...

    Cheers, Tobi

    October 2nd, 2009 @ 10:47 AM

  2. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:41 AM

  3. erik landerholm
    erik landerholm

    I would recommend changing the Hash.new into an OrderedHash. I know this works in rails 2.3.8. mysql is specific about the order of it's joins. using a hash could result into random failures if you are using joins in your scope.

    for 2.3.8 change
    joins.flatten.inject(Hash.new)....

    to
    joins.flatten.inject(ActiveSupport::OrderedHash.new)

    thanks.

    March 30th, 2011 @ 09:23 PM