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
def get_recording_list_by(team_members) do
team_members_ids = Enum.map(team_members, fn team_member -> team_member.user.id end)
recording_query =
from(
r in Recording,
preload: [:user],
where: r.user_id in ^team_members_ids,
select: r
)
Repo.all(recording_query)
end
OK something new, I commented out the ecto.assoc
and now this is the error at the end:
lib/userteam1_web/controllers/recording_controller.ex:30: value `[1]` in `where` cannot be cast to type :id in query:
from r in Userteam1.Web.Recording,
where: r.user_id == ^[1],
select: r,
preload: [:user]
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
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