Ash FrameworkAF
Ash Framework7mo ago
13 replies
bhelms

How do I get `fragment` in scope in a `prepare` block?

I'm wanting to sort a query using a fragment. I found this https://elixirforum.com/t/how-do-i-sort-records-by-random-in-the-resource-action/58774 that looks like it would work. However, I get an error when I try.
My code:
read :my_action do
  argument :embedding, :vector, allow_nil?: false 
  prepare fn query, _ ->
     Ash.Query.sort(query, Ash.Sort.expr_sort(fragment("1 - (embedding <=> ?)", arg(:embedding))))
  end
end

The error I get error: undefined function fragment/2 (expected MyModule to define such a function or for it to be imported, but none are available).
How do I use fragment in the prepare function? Or is there a better way to do this?
Was this page helpful?