sezaru

sezaru

How to run a second query inside a read action?

Hey, I have a read query that uses pg_trgm and I want to set word_similarity_threshold before running it.

I tried something like this:

    read :by_entity do
      argument :entity_full_name, :string, allow_nil?: false

      prepare fn query, context ->
        Ash.Query.before_action(query, fn query ->
          Pacman.Repo.query!("set pg_trgm.word_similarity_threshold = 0.99") |> dbg()

          query
        end)
      end

      filter expr(fragment("(? %> ?)", first_grantee_full_name, ^arg(:entity_full_name))
                  and not is_nil(recording_date || instrument_date)
                  and (recording_date || instrument_date) >= ago(2, :year)
                  and not deleted?)

      prepare build(sort: [:recording_date])
    end

But this doesn’t seem to work, I believe because there is no commit query encapsulating both queries inside the same transaction.

What would be the best way to achieve this with Ash?

Marked As Solved

sezaru

sezaru

Oh, I just found out that I can pass transaction? true to the read action and that will make the read action be encapsulated inside a transaction making my preparation work as expected.

Since that is fixed, just another question, would Pacman.Repo.query!("set pg_trgm.word_similarity_threshold = 0.99") be the best way to run such a query or is there a better way to do it with Ash?

Also Liked

zachdaniel

zachdaniel

Creator of Ash

That would be the best way :slight_smile:

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement