This project is archived and is in readonly mode.
[PATCH] Merging JOINs when chaining scopes requires same case in JOIN expression
Reported by Matthias | October 1st, 2009 @ 11:09 AM
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.
Comments and changes to this ticket
-
Tobi October 2nd, 2009 @ 10:47 AM
- 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
-
Rohit Arondekar October 6th, 2010 @ 06:41 AM
- 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.
-
erik landerholm March 30th, 2011 @ 09:23 PM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>