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
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
0
Also Liked
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
1
Last Post!
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
0
Popular in Questions
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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
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
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
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
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









