Home > Software design >  Arel left outer join on second level table
Arel left outer join on second level table

Time:12-19

A.joins(b: :c)

but I want the (b: :c) to be b.left_outer_join(:c)

any ideas please?

CodePudding user response:

You can do it by manually appending the b LEFT OUTER JOIN with c:

a = A.arel_table
b = B.arel_table
c = C.arel_table

a
  .join(b)
  .on(a[:id].eq(b[:a_id]))
  .join(c, Arel::Nodes::OuterJoin)
  .on(b[:id].eq(c[:b_id])).to_sql

Which gives

SELECT
FROM "a"
INNER JOIN "b" ON "a"."id" = "b"."a_id"
LEFT OUTER JOIN "c" ON "b"."id" = "c"."b_id"

CodePudding user response:

Or...

b = B.arel_table
c = C.arel_table

join_on = b.create_on(b[:c_id].eq(c[:id]))
bc_join = b.create_join(c, join_on, Arel::Nodes::OuterJoin)

result = A.joins(b).joins(bc_join)
  • Related