Read action that does a contains search

I’m sure this must be a trivial thing to do, but in looking through the documentation so far I have not figured this out. I’m trying to make a simple read action that does a substring search and the syntax is just not clear to me. Feel free to RTFM me, but please point me to the right place in the docs :slight_smile:

Here’s what I’ve tried:

    read :query do
      argument :query, :string, allow_nil?: false

      filter expr(contains(name, ^arg[:query]))
    end

This fails compilation with undefined function contains/2 (there is no such import)

:thinking:pretty sure that’s exactly how you do it. Is the read action accidentally outside of the actions block perhaps?

Oh, wait it’s ^arg(:query)

Doesn’t explain the error about contains/2 though

As for documentation:

Examples in Ash.Query docs: Ash.Query — ash v3.2.0

Examples in the Ash.Resource DSL docs: DSL: Ash.Resource.Dsl — ash v3.2.0

Expression reference: Expressions — ash v3.2.0

Yup, this was it! Derp. I need to pair more :wink:

The contains thing was weird though. I swear I copied that from the terminal, but when I put the code back to what I had, now I get much more helpful error about undefined variable arg. At least that probably would have pointed me in the right direction.

For posterity, this worked like a champ:

    read :query do
      argument :query, :string, allow_nil?: false

      filter expr(contains(string_downcase(name), string_downcase(^arg(:query))))
    end

I’m sure this can be cleaned up a little, but in case someone stumbles upon this looking for the example I was looking for its a start.

2 Likes