I am trying to avoid string interpolating my joins
in Rails because I've noticed a decrease in flexibility when chaining queriers together.
That is, I feel that joins(:table1)
is much more flexible than joins('inner join table1 on table1.id = this_table.table1_id')
.
What I would like to accomplish is:
FROM table1
INNER JOIN table2 on table2.id = table1.table2_id
LEFT JOIN table3 on table3.id = table2.table3_id
inner join all
However, I can't figure out how to do it using Rails parlance:
Table1.joins(table2: :table3)
Results in an INNER join on the final table.
FROM table1
INNER JOIN table2 on table2.id = table1.table2_id
INNER JOIN table3 on table3.id = table2.table3_id
left joins all
If I use left_outer_joins
...
Table1.left_joins(table2: :table3)
Results in LEFT joins on both tables (unwanted).
FROM table1
LEFT JOIN table2 on table2.id = table1.table2_id
LEFT JOIN table3 on table3.id = table2.table3_id
how to mix joins ?
Can't seem to chain joins without specify the relations ... that is, these don't work:
Table1.joins(:table2).left_join(table2: :table3)
Table1.joins(:table2).left_join(:table3)
Is there any way to do this the way I want?
find_by_sql
? – Harberjoins
from your post ! – Harber