lardcanoe
Ash resource filtering embedded map with user input
I am trying to parse a user generated filter that will apply to jsonb field (that is just a map, not an embedded resource).
The field is data, and if I include it in the statement sent to parse_input I get an Ash.Error.Query.NoSuchFilterPredicate error.
If I exclude that field, and instead manually apply it, |> Ash.Query.filter(data[:size] == ^"Big") then things works.
Is there a syntax to allow passing fields of a map into the parse function? If not, how can I pass it to Ash.Query.filter? To make it slightly more complicated, I would also like to be able to filter on the data field when it is on a reference as well. e.g. `%{person: %{data: %{size: “Big”}}}
statement = %{
person: %{parent: %{name: "Daniels"}},
data: %{size: "Big"}
}
# This will blow up with the above
{:ok, filter} = Ash.Filter.parse_input(resource, statement)
# If I remove "data" and instead do it manually, then this works:
resource
|> Ash.Query.for_read(read_action, %{}, actor: actor, tenant: tenant)
|> Ash.Query.filter_input(filter)
|> Ash.Query.filter(data[:size] == ^"King")
Filtering on embedded resource? - #6 by zachdaniel was where I saw the Ash.Query.filter approach on arbitrary fields of a jsonb attribute; I just couldn’t figure out how to pass my user generated filter since that is a macro.
Thank you!
Marked As Solved
zachdaniel
So, I made that change and realized that it just kind of shuffles the problem around, because if you have an attribute called at_path it would be problematic…but actually I think it’s fine. The reason its fine is that at_path expects a list, and if you need to match on it you can say at_path: %{eq: ["foo"]}, so there is an escape hatch (albeit a bit of a convoluted one)
Also Liked
zachdaniel
Alright, I’ve pushed this change to main (and removed the previous change I added)
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








