Skip to content

Commit

Permalink
Pass join parameters on assoc with :on forward, closes #1678
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Sep 8, 2016
1 parent 066c62d commit 6022e9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions integration_test/cases/joins.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ defmodule Ecto.Integration.JoinsTest do

query = from(p in Post, join: c in assoc(p, :permalink), order_by: p.id, select: {p, c})
assert [{^p2, ^c1}] = TestRepo.all(query)

query = from(p in Post, join: c in assoc(p, :permalink), on: c.id == ^c1.id, select: {p, c})
assert [{^p2, ^c1}] = TestRepo.all(query)
end

@tag :left_join
Expand Down
14 changes: 7 additions & 7 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ defmodule Ecto.Query.Planner do
defp attach_on(joins, %{expr: true}) do
joins
end
defp attach_on([h|t], %{expr: expr}) do
h =
update_in h.on.expr, fn
true -> expr
current -> {:and, [], [current, expr]}
end
[h|t]
defp attach_on([%{on: %{expr: true}} = h|t], on) do
[%{h | on: on}|t]
end
defp attach_on([h|t], %{expr: extra_expr, params: extra_params}) do
%{on: %{expr: expr, params: params} = on} = h
on = %{on | expr: {:and, [], [expr, extra_expr]}, params: params ++ extra_params}
[%{h | on: on}|t]
end

defp rewrite_join(%{on: on, ix: join_ix} = join, qual, ix, last_ix, source_ix, inc_ix) do
Expand Down

0 comments on commit 6022e9d

Please sign in to comment.