Dynamically select fields with "where" in Ecto query

Hello! I’m trying to include a dynamic selection field to an Ecto query, instead of a hard-coded one. So instead of manually running a query & update for every field, I can just pass in a list of atom fields and have it run through dynamically:

for field <- list_of_fields do
from(t in Post, where: ^t[field] < 0) |> Repo.update_all(set: [{field, 0}])
end

But running this gives me (CompileError) iex:5: undefined function t/0 (there is no such import)

Is there any way to dynamically query on fields like this? Have tried a few different ways but no luck.

@warronbebster see: Ecto.Query.API — Ecto v3.9.4

1 Like

Yes! thank you!