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

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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

electic
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
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
jononomo
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement