benonymus
Ecto query for list of items' ids
Hey I am returning a list of users from a function, and I want to use that list for a query.
how could I use the ids of those items for the query selection?
def get_recording_list_by(team_members) do
recording_query =
from(
r in Recording,
preload: [:user],
where: r.user_id == ^team_members.user.id, --- questionable part
select: r
)
Repo.all(recording_query)
end
Marked As Solved
benonymus
fixed it by using in instead of ==
so the final code looks like this:
def get_recording_list_by_team_id(team_members) do
IO.inspect(team_members)
team_members_ids = Enum.map(team_members, fn team_member -> team_member.id end)
IO.inspect(team_members_ids)
recording_query =
from(
r in Recording,
preload: [:user],
where: r.user_id in ^team_members_ids,
select: r
)
Repo.all(recording_query)
end
Also Liked
LostKobrakai
Or just recording_query = from Ecto.assoc(team_members, :recording), preload: [:user]
peerreynders
https://hexdocs.pm/ecto/Ecto.Query.API.html#in/2
though you’re possibly talking about from p in Post
https://hexdocs.pm/ecto/Ecto.Query.html#from/2
In general the in expression is looking for an Ecto.Queryable on the right hand side - if it’s a string it is referring to a DB table. The Ecto.Queryable is usually a module that uses Ecto.Schema
alexandre
Can anybody point to me where this IN is documented in ecto? I cannot find, and it took me so long to find this post ![]()
benonymus
I’m glad my post helped you as well as it helped me ![]()







