Skip to content

Instantly share code, notes, and snippets.

@rbishop
Last active August 29, 2015 14:06
Show Gist options
  • Save rbishop/9625b147f4fa761d0ef0 to your computer and use it in GitHub Desktop.
Save rbishop/9625b147f4fa761d0ef0 to your computer and use it in GitHub Desktop.
Based on an unknown list size, I want to create an inner join and where clause for each item in the list. I don't know how to dynamically change the binding in the where, though.
def by_path(ancestry) do
Enum.reduce(ancestry, App.Node, fn (ancestor, query) ->
query
|> join(:inner, [n], n1 in App.Node, n1.id == n.parent_id)
|> where([n], n.key == ^ancestor) # This is where I'm stuck, all the where's are n0."key", see line 14
end)
end
# Here is the resulting SQL:
SELECT n0."id", n0."key", n0."project_id", n0."type", n0."parent_id"
FROM "nodes" AS n0
INNER JOIN "nodes" AS n1 ON n1."id" = n0."parent_id"
INNER JOIN "nodes" AS n2 ON n2."id" = n0."parent_id"
WHERE (n0."key" = $1::text) AND (n0."key" = $2::text) AND (n0."key" = $3::text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment