Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Last active October 11, 2017 14:34
Show Gist options
  • Save coryodaniel/49329f99ffd7704cc0d7bd9bcdd7ef47 to your computer and use it in GitHub Desktop.
Save coryodaniel/49329f99ffd7704cc0d7bd9bcdd7ef47 to your computer and use it in GitHub Desktop.
Virtual / computed columns in Ecto / Elixir
schema "comments" do
field :text, :string
# ... more schema ...
field :ancestor_id, :integer, virtual: true
end
def thread(comment) do
from c in Comment,
join: cp in CommentPath, on: c.id == cp.descendant_id,
where: cp.ancestor_id == ^comment.id,
select: %{c | ancestor_id: cp.ancestor_id}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment