sooon
sooon

Reputation: 4888

elixir ecto - pass preloaded data in to embeded preloaded subquery

I have 3 tables:

Users will work on the Tasks, and will get Reviews for how well he did the job. The Reviews is towards the Users, rather than the Tasks, because it is the Users' workmanship.

I have this query:

rq = from r in Review,
  where: r.module_type == "users",
  where: r.module_id == ^user_id,
  group_by: r.module_id,
  select: avg(r.stars)

t =
  Repo.get_by(Task, id: task_id)
  |> Repo.preload(user: [reviews: rq])

Obviously, I am getting an error:

undefined function user_id/0

My question is, how can I pass the user_id from prelaoded user into rq the subquery?

I know we can use tuple in the preload to put more details. But I am not sure how to do so.

Upvotes: 0

Views: 125

Answers (1)

narrowtux
narrowtux

Reputation: 703

You don't need to. If the value in the preloads list should be a query, like you've done, ecto will automatically add the correct filter by itself.

Upvotes: 0

Related Questions