ryanditjia
How do I query a many-to-many relationship the Ecto way
Hello peeps, I feel this query isn’t concise (or isn’t Ecto-y) enough:
def list_posts_for(tag_id) do
query =
from(pt in PostTag,
where: pt.tag_id == ^tag_id,
left_join: p in Post,
on: p.id == pt.post_id,
select: p
)
Repo.all(query)
end
Posts and tags have many to many relationships + a join schema, and Ecto knows all about them.
Is it possible to start the query with from p in Post? Other ideas are welcome too.
First Post!
c4710n
Yes, you can.
Query post with id, and preload tags:
from p in Post,
join: t in assoc(p, :tags),
where: p.id == 1,
preload: [tags: p]
But, personally, I would like to use preload directly:
Repo.get(Post, 1) |> Repo.preload(:tags)
If you need tags associated with a post only, try
Ecto.assoc/2.
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I would like to know what is the best IDE for elixir development?
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







