LeJoko
LeJoko
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
You answered me in the past that a MySQL connector is planned and I saw in a recent video that it is on your near future roadmap . Do you have an estimation as to when may do it ? Even a rough one... (it may be a deal breaker for me)
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
Understood. Thank you very much for your help.
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
The problem I have with your suggestion is that it works only if the total can be easily incremented by the amount. If it would require a complex calculation, it might not be so easy.
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
Aren't Ash update actions transactional ? If I'd lock the parent record in a before_action (or with an around_action) shouldn't that take care of the race condition ?
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
By the way, what result an after_action is supposed to return ? (when returning `{:ok, resource} ) the doc is not clear about how and where that result may be used.
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
It works but feels a bit clunky. What problems do you see with it ?
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
change after_action(fn _changeset, result ->
MyApi.load!(result, :parent).parent
|> Ash.Changeset.for_action(:update_total)
|> Parent.update!()
{:ok, result}
end)
change after_action(fn _changeset, result ->
MyApi.load!(result, :parent).parent
|> Ash.Changeset.for_action(:update_total)
|> Parent.update!()
{:ok, result}
end)
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
In child:
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
actions do
update :update_totals do
change(fn changeset, _context ->
parent = MyApi.load!(changeset.data, [:total_from_aggregate])
changeset
|> Ash.Changeset.change_attributes(%{
recorded_total: parent.total_from_aggregate,
})
end)
end
end

aggregates do
sum :total_from_aggregate, :children, :amount
end
actions do
update :update_totals do
change(fn changeset, _context ->
parent = MyApi.load!(changeset.data, [:total_from_aggregate])
changeset
|> Ash.Changeset.change_attributes(%{
recorded_total: parent.total_from_aggregate,
})
end)
end
end

aggregates do
sum :total_from_aggregate, :children, :amount
end
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
In parent :
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
In my previous trials I did approximately that:
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
I tried something like that : I made an update_total action in the invoice resource that does the job of updating the total_amount field (using an aggregate) and in the lines resource I made a change in a changes block that calls the update_caches action on the parent invoice in an after_action. It seems ok, but I've a feeling that something might be done better.
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
Another example could be a tree of related resources where the lowest ones need to "touch" their parent recursively to mark them as "updated"
27 replies
AEAsh Elixir
Created by LeJoko on 4/24/2023 in #support
What is the "Ash way" to auto update related resources ?
I need to have it recorded. It is definitely an optimization. I tried it only with aggregates and calculations and it does work very well (I love them!), but if I continue that project, in the longer run, I will have fields that require complex calculations dependent on business rules and also need to be queryable fast. So I'd be very happy to understand how it could be done "the Ash way".
27 replies