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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
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

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
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
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

We're in Beta

About us Mission Statement