Hi, I have a Resource called Team
which has a TeamMember
relationship :members
.
Is there a way to use change manage_relationship(...)
and apply an update action to all members?
E.g.
# Team.ex
update :activate_all_members do
change manage_relationship(:members, ..., type: :update, action: activate)
end
The only alternative I can see right now is to implement a manual action, but is that really needed?
Definitely no need to do write a manual action. You can do everything you want with a change and a hook
defmodule ActivateAllMembers do
use Ash.Resource.Change
require Ash.Query
def change(changeset, _, _) do
Ash.Changeset.after_action(changeset, fn changeset, result ->
Member
|> Ash.Query.filter(team_id == ^result.id)
|> Ash.bulk_update!(:activate, %{})
{:ok, result}
end)
end
def atomic(changeset, opts, context), do: {:ok, change(changeset, opts, context)}
end
update :activate_all_members do
change ActiveAllMembers
end