Reputation: 8461
I'm trying to figure out how to scope queries for records that belong to a particular user. When I pass in the scoped records I get this error:
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for [%Dreamhouse.Project{__meta__: #Ecto.Schema.Metadata<:loaded, "projects">, id: 878, inserted_at: ~N[2018-05-09 18:13:22.820266], rows: #Ecto.Association.NotLoaded<association :rows is not loaded>, title: "Test Title", updated_at: ~N[2018-05-09 18:13:22.820274], user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1118}]. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple
Here is my controller code:
user_id = Map.get(Dreamhouse.Guardian.Plug.current_resource(conn), :id)
user = Repo.get(User, user_id)
projects = Repo.all(from p in assoc(user, :projects))
query =
from(
p in projects,
left_join: r in assoc(p, :rows),
left_join: i in assoc(r, :images),
order_by: r.index,
order_by: i.index,
preload: [rows: {r, images: i}]
)
projects = Repo.all(query)
It happens when I look for p in projects
. I see that it doesn't like that I'm passing in a list. But What does it expect in order to create the query?
Upvotes: 0
Views: 298
Reputation: 222418
Repo.all
executes the query and returns a list of structs. To use this as the source of another query, you need to pass the raw query, not a list of structs. Removing Repo.all
should fix this:
projects = from(p in assoc(user, :projects))
Upvotes: 1