FlyingNoodle

FlyingNoodle

"Computed" properties in Ash?

Does ash have the concept of “computed” fields? Ie something like

attributes do
  attribute :status, :atom, constraints: [:open, :closed]

  # i know this doesnt exist
  computed :is_open?, :boolean, computation: expr(status == :open)
end

I’m asking because I have a calculation on a resource which is incorrect after updating the resource. Manually refetching all possible calculations seems very error prone and can’t be the recommended way. I saw in the documentation that there is a generated? setting on an attribute but the explanation Whether or not the value may be generated by the data layer. doesnt tell me too much.

My “mutation” looks like this:

ticket =
  Support.Ticket.get_by_id!(ticket_id)
  |> Support.Ticket.toggle!()
  |> Support.load!(:is_open?)

However, this means that I would have to remember to include all the calculations that might possible be needed.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

I see. There are some options.

You can add a global preparation and a global change to ensure that a given calculation is always loaded, like so:

preparations do
  prepare build(load: :calc)
end

changes do
  change load(:calc)
end
  1. you can calculate the thing you need on demand:
Api.calculate(record, :is_open?)

And if you want a named function for that, you can put it in your code interface:

code_interface do
  define_for YourApi

  define_calculation :is_open?, args: [:_record]
end

which will give you YourResource.is_open?(your_record).

In cases where it is not necessary, this will not visit the data layer (i.e in your case we have the status, so we compute the result in Elixir). However, loading up front is often good in case calculations may need to do something expensive, you can do them in one place up front.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New

We're in Beta

About us Mission Statement