Skip to content

Instantly share code, notes, and snippets.

@rymai
Created December 1, 2010 16:49
Show Gist options
  • Save rymai/723780 to your computer and use it in GitHub Desktop.
Save rymai/723780 to your computer and use it in GitHub Desktop.
Arel 2.0.5 LEFT OUTER JOIN bug
TableA
has_and_belongs_to_many :table_b
TableB
has_and_belongs_to_many :table_a
TableC is the join table of TableA and TableB
---------------------------------------
Arel 2.0.4 (Good query)
SELECT COUNT(DISTINCT "table_a"."id")
FROM "table_a"
LEFT OUTER JOIN "table_c" ON
"table_c"."table_a_id" = "table_a"."id"
LEFT OUTER JOIN "table_b" ON
"table_b"."id" = "table_c"."table_b_id"
---------------------------------------
Arel 2.0.5 (Fucked-up query)
SELECT COUNT(DISTINCT "table_a"."id")
FROM "table_a"
LEFT OUTER JOIN "table_c" ON
'table_c' AND "table_c" AND 'table_a_id' AND "table_c"."table_a_id" AND 0 AND "table_a" AND 'id' AND "table_a"."id" AND
"table_c"."table_a_id" = "table_a"."id"
LEFT OUTER JOIN "table_b" ON
0 AND "table_b" AND 'id' AND "table_b"."id" AND 0 AND "table_c" AND 'table_b_id' AND "table_c"."table_b_id" AND
"table_b"."id" = "table_c"."table_b_id"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment