rapidfsub

rapidfsub

Best practice for after_action?

In after_action change, I usually load some relationships and call some update actions.
And the action would return a dirty record with old relationships.

Example

defmodule A do
  actions do
    update :update do
      change after_action(fn changeset, record, context ->
        record = Ash.load(record, [:b])
        B.update!(record.b, params)
        {:ok, record}
      end)
    end
  end
end

A.update action calls B.update in after_action.
The result of B.update is not saved anywhere but db.
Therefore, A.update action would return updated A record and old B record.
Should I always call Ash.reload!(record) at the end of after_action?
Or, is there any recommendation how to use after_action?

This is problematic when calling Ash.can with returned A record.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash
defmodule A do
  actions do
    update :update do
      change after_action(fn changeset, record, context ->
        record = Ash.load(record, [:b])
        # you can modify it directly
        {:ok, %{record | b: B.update!(record.b, params)}}
      end)
    end
  end
end

Last Post!

rapidfsub

rapidfsub

Thanks. I thought it could be an antipattern, by not using functions in Ash.
But it wasn’t. :rofl:

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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

We're in Beta

About us Mission Statement