Possible to relate to actor on join table for many-to-many relationship?

So I am building a backend using Ash where Users (using Ash Authentication) can create and be members of a project and each project can have more than one member.

On project resource:

  relationships do

    has_many :project_members, ProjectHero.Projects.ProjectMember do
      destination_attribute :project_id
    end

    many_to_many :members, ProjectHero.Accounts.User do
      through ProjectHero.Projects.ProjectMember
      source_attribute_on_join_resource :project_id
      destination_attribute_on_join_resource :user_id
    end
  end

And on project_member resource:

  relationships do
    belongs_to :project, ProjectHero.Projects.Project, primary_key?: true, allow_nil?: false
    belongs_to :user, ProjectHero.Accounts.User, primary_key?: true, allow_nil?: false
  end

On my create action for projects I do this:

    create :create do
      accept [:name]

      argument :user_id, :uuid do
         allow_nil? false
      end

      change manage_relationship(:user_id, :members, type: :append_and_remove)
    end

And it works creating the project_member. However, it means I manually have to provide the user_id even if that already exits in he actor.

I tried this:

      change fn changeset, context ->
        change manage_relationship(context.actor.id, :members, type: :append_and_remove)
      end

But that does not work. Is there anyway to do related actor but for a many-to-many relationship on a join table?

change is used in the DSL for constructing a resource, but in this context, you’ll need to use functions provided by Ash.Changeset

change fn changeset, context ->
  Ash.Changeset.manage_relationship(changeset, :members, context.actor.id, type: :append_and_remove)
end
1 Like

Thanks! That did it!