Simple manage_relationship with additional field to set

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!

belongs_to :user creates a non-public user_id attribute. I’m assuming that the relevant create action on the UserOrganization resource has either no accept list, or is using accept :*. So you’ll either want to accept :* in the create action, or mark the relationship as public? true.

Thanks Zach, your assumptions are correct.

I marked the user relationship in UserOrganization as public? true and set accept :* for the default create action.

I need to get my knowledge up to date with Ash v3.