Metaprogramming with expressions

I am generating a module at compile time, and my Ash.Query.filter doesn't work as intended. Please see comment below
defp generate_oban_worker_module(module, cache) do
contents =
quote do
require Ash.Query
use Oban.Worker, ...

@impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => id}}) do
record =
unquote(cache.module)
# The following line doesn't work.
# It is evaluating to true and returning all records
|> Ash.Query.filter(id == ^id)
|> unquote(cache.api).read_one!(authorize?: false)
...
end

Module.create(
worker_name(module, cache),
contents,
Macro.Env.location(__ENV__)
)
end
defp generate_oban_worker_module(module, cache) do
contents =
quote do
require Ash.Query
use Oban.Worker, ...

@impl Oban.Worker
def perform(%Oban.Job{args: %{"id" => id}}) do
record =
unquote(cache.module)
# The following line doesn't work.
# It is evaluating to true and returning all records
|> Ash.Query.filter(id == ^id)
|> unquote(cache.api).read_one!(authorize?: false)
...
end

Module.create(
worker_name(module, cache),
contents,
Macro.Env.location(__ENV__)
)
end
2 Replies
Alan Heywood
Alan HeywoodOP•2y ago
Found it 😀 – I had to use ref like this: Ash.Query.filter(ref(:id) == ^id)
ZachDaniel
ZachDaniel•2y ago
yep!

Did you find this page helpful?