Ash FrameworkAF
Ash Framework3y ago
30 replies
l00ker

Calculate only on a read action

Hello everyone. Just starting out with Ash and so far, so good. I'm starting to figure a few things out. What I'm currently struggling with is having a calculate only run on a read action.

I have an embedded resource where I'm using a calculate to generate a URL from another field. Here is a snippet from parent resource where the embedded is loaded:

  attributes do
    ...
    attribute :myembed, MyEmbed do
      constraints [load: [:url]]
      allow_nil? false
    end
    ...
  end


That works and I get the calculated :url field injected into the embedded resource when reading items. Yay! My issue is that the :url field is also being written to the embedded field on create.

How can I have the calculate run only during the read actions?

I tried using a various Ash.Query functions in aprepare in the read action of the embedded resource as well as in the read action of the parent, but couldn't get it load and/or not generate errors.
Was this page helpful?