Create a gist now

Instantly share code, notes, and snippets.

ecto get parents/children
defmodule Node do
import Ecto.Query, only: [from: 2]
def get_children(node) do
from(n in Node,
join: n2n in NodeToNode, on: n.id == n2n.parent_id,
inner_join: n1 in Node, on: n1.id == n2n.child_id,
select: n1,
where: n.id == ^node.id)
|> Repo.all
end
def get_parents(node) do
from(n in Node,
join: n2n in NodeToNode, on: n.id == n2n.child_id,
inner_join: n1 in Node, on: n1.id == n2n.parent_id,
select: n1,
where: n.id == ^node.id)
|> Repo.all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment