I'm using Ecto to request data from the database, and I've used the following code to preload the cats filtered by price_discount.
Item
|> join(:inner, [i], c in Cat, c.food_category_id == i.id)
|> where([i, c], is_nil(c.price_discount))
|> preload([i, c],
[
cats: c,
cats: :houses
])
This works great, but now I want to order Houses by id, so I was hopping this work:
Item
|> join(:inner, [i], c in Cat, c.food_category_id == i.id)
|> where([i, c], is_nil(c.price_discount))
|> preload([i, c],
[
cats: c,
cats: [
houses: from(h in Houses, order_by: h.id)
]
])
But it doesn't, this is the error:
from(h in Houses, order_by: h.id()) is not a valid preload expression. preload expects an atom, a list of atoms or a keyword list with more preloads as values. Use ^ on the outermost preload to interpolate a value
The only post to work is to just use one of the following:
- Use
cats: c
without the order - Use
houses: from(h in Houses, order_by: h.id)
The following works, using ^:
Item
|> join(:inner, [i], c in Cat, c.food_category_id == i.id)
|> where([i, c], is_nil(c.price_discount))
|> preload([i, c],
^[
cats: [
houses: from(h in Houses, order_by: h.id)
]
])
Is this a bug in Ecto ?
cats: {c, houses: from(h in Houses, order_by: h.id)}
? – Fultzcats: {c, houses: ^from(h in Houses, order_by: h.id)}
is the correct format. I didn't know how to use {} in preload, now I understand better. – Trapan