I’m playing aorund with Ecto Dynamic to enable filtering on any column. I have the following code which works perfectly (it was taken pretty much as is from the docs)
def filter(filters) do
filter =
Enum.reduce(
filters,
dynamic(true),
fn
{:code, value}, dynamic ->
dynamic([n], ^dynamic and like(p.code, ^value))
{:name, value}, dynamic ->
dynamic([n], ^dynamic and like(p.name, ^value))
{_, _}, dynamic ->
dynamic
end
)
from(
p in Post,
where: ^filter,
preload: [:comments]
)
|> Repo.all()
end
I’d like to be able to extend the filtering to the preloaded comments - so I could filter on Post.name AND comment.foo.
Is there a way to rewrite the above code to enable a filter on the :comments preload (as well as the parent table) or do I have to do this as a seperate :comment query?
Thanks @fuelen - I had a read and quick attempt but couldn’t get it to work. I’ll give it another go at lunchtime (but any clues as to what the code should look like will be gratefully recevied :- )