spacebat

spacebat

Read action by related struct

I have a belongs_to :definition relationship on a resource and can read all the resource instances belonging to a definition with:

    read :by_definition_id do
      argument :definition_id, :string
      filter expr(definition_id == ^arg(:definition_id))
    end

I figured it would be nice to have something like:

    read :by_definition do
      argument :definition, :map
      filter expr(definition_id == ^arg(:definition)[:id])
    end

However I can’t get this to compile, nor using .id or various other combinations. I just can’t seem to connect the definition argument’s id attribute to the filter expression.

Marked As Solved

spacebat

spacebat

Thanks @barnabasJ that got me on the right track:

    require Ash.Query
    read :by_definition do
      argument :definition, :map

      prepare fn query, _context ->
        definition_id = Ash.Query.get_argument(query, :definition).id
        Ash.Query.filter(query, definition_id == ^definition_id)
      end
    end

Also Liked

barnabasJ

barnabasJ

Ash Core Team

You might need to do it in a preparation to get to the value

    read :by_definition do
      argument :definition, :map

      prepare fn query, _ ->
        argument_definition_id = Ash.Query.get_argument(query, :definition)[:id]
        query
        |> Ash.Query.filter(definition_id == ^argument_definition_id) 
      end
    end

Last Post!

zachdaniel

zachdaniel

Creator of Ash

If this was something you wanted to be able to use throughout your application, you could define a custom type and a custom preparation and get pretty elegant usage:

    read :by_definition do
      # you can get custom short names with a configuration, so you can do this w/o anything built into ash
      argument :thing, :struct_or_id, constraints: [for: TargetResource]

      # you can import functions that return `{Module, opts}`, just like builtins
      prepare filter_by_struct_or_id(:definition_id, :thing)
  end

Where Next?

Popular in Questions Top

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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics 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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement