Hello,
I have a simple manage_relationship to do but I cannot figure out how to do it.
This is the relationship, a many to many with intermediate resource:
Organization <- UserOrganization -> User
I want to have a simple “initialize” action that creates an Organization and connects it to the User with the addition of setting the role to “owner”.
defmodule Organization do
...
create :initialize do
accept [:name]
argument :owner_id, :uuid do
allow_nil? false
end
change fn changeset, _ctx ->
Ash.Changeset.manage_relationship(changeset, :users_organizations, [%{user_id: arg(:owner_id), role: :owner}], type: :create)
end
end
...
relationships do
has_many :users_organizations, UserOrganization
many_to_many :users, User do
through(UserOrganization)
join_relationship :users_organizations
end
end
Error:
%Ash.Error.Invalid{
errors: [%Ash.Error.Changes.Required{field: :user, type: :relationship, resource: nil, splode: Ash.Error, bread_crumbs: [], vars: [], path: [:users_organizations, 0], ... ]
}
In UserOrganization I have:
relationships do
belongs_to :user, User, allow_nil?: false
belongs_to :organization, Organization, allow_nil?: false
end
Thank you for your help!