Ash FrameworkAF
Ash Framework8mo ago
15 replies
Vahagn

manage_relationship to create another resource `belongs_to` relation

Hi, I noticed I have a pretty common thing to define an action on a resource, which will automatically created related resources that is in the scope of the action. I wanted to know how to do that declaratively

Example.
Organization (has_many Member)
Member (belongs_to Organization, belongs_to User)
User (has_many Member)


I want to define a :create on Organization, that will create a member with actor
Currently I am doing it in a custom function that calls
change fn changeset, context ->
  Ash.Changeset.manage_relationship(
    changeset,
    :members,
    %{user_id: context.actor.id, role: "owner"},
    type: :create
  )
end

But I don't like this way, as there is a manage_relationship Resource action, but I don't want anything passed as arguments
Solution
manage_relationship will eventually call a create/update action etc., and you are providing inputs for it here
Was this page helpful?