Use change manage_relationship without argument in create action.

I have a resource called User which has a has_one relationship with another resource called ImpersonatePermissions. When I create a new user, I want to also create a new in the impersonate_permissions table. To do that, I believe that I need to use manager_relationship. I was able to make this work with a custom change function:
change fn changeset, %{actor: actor} ->
Ash.Changeset.before_action(changeset, fn changeset ->
args = %{place_offer?: true, organization_id: actor.organization_id}

Ash.Changeset.manage_relationship(changeset, :impersonate_permissions, args,
type: :create
)
end)
end
change fn changeset, %{actor: actor} ->
Ash.Changeset.before_action(changeset, fn changeset ->
args = %{place_offer?: true, organization_id: actor.organization_id}

Ash.Changeset.manage_relationship(changeset, :impersonate_permissions, args,
type: :create
)
end)
end
This works, but I was wondering if there is a better/simpler way to do this, I think that probably there is a way to translate this into a change manage_relationship call, but all the example I found uses an action input argument to set the args, and I don't want that since I don't want the caller to be able to change the arguments. I tried this but don't seem to work since the function expects a string as the second argument:
change manage_relationship(:impersonate_permissions, %{place_offer?: true, organization_id: actor(:organization_id)}, type: :create)
change manage_relationship(:impersonate_permissions, %{place_offer?: true, organization_id: actor(:organization_id)}, type: :create)
Any suggestions?
1 Reply
ZachDaniel
ZachDaniel3y ago
The way that you did it is the best way to do it

Did you find this page helpful?