Hi all,
I’ve been really enjoying learning Elixir, Phoenix, and Ecto. Spending a lot of time in the docs. I’m wondering what the best practice for writing queries.
I wrote a query this way because it was closer to SQL and easier for me to think in, over Elixir/Phoenix approaches.
# SQL-like Version
Repo.all(
from u in User,
where: u.id not in subquery(
from p in Participant,
where: p.meeting_id == ^meeting_id,
select: p.user_id
)
)
But I wasn’t satisfied with not giving it a more Elixir/Phoenix look and feel using pipes. So I came up with this:
# Pipe Operator Version
User
|> where([u], u.id not in subquery(
Participant
|> where([p], p.meeting_id == ^meeting_id)
|> select([p], [p.user_id])
)) |> Repo.all
I’ll admit, I’m kind of hooked on the |>
operator, but looking at that code it doesn’t look as clean as the first way.
Aesthetics aside, I’m wondering is there a reason to choose one way over the other. Also, keen to hear any feedback on the Pipe Operator Version (e.g., is there a better or cleaner way to write it).
Cheers!