Ash FrameworkAF
Ash Framework7mo ago
4 replies
srmico

Managing many_to_many relationships with AshPhoenix.Form

Hi all, I'm really struggling to get AshPhoenix.Form to manage a many to many relationship.

I have 2 resources, Organizations and Users. I have a many_to_many relationship between them. What I'm trying to achieve, is that when I create/edit an organization, there should be a multi select to be able to select related users.
create :create do
  accept [:name]
  argument :users, {:array, :map}
  change manage_relationship(:users,
           on_lookup: :relate,
           on_no_match: :error,
           on_match: :ignore,
           on_missing: :error
         )
end

From that I autogenerate the liveview with ash_phoenix.gen.live.
It correctly creates a form with a multi select, but the options for it are empty.
      <.form for={@form} id="organizations-form" phx-change="validate" phx-submit="save">
        <%= if @form.source.type == :create do %>
          <.input
            field={@form[:users]}
            type="select"
            multiple
            label="Users"
            options={[]}
          />
          <.input field={@form[:name]} type="text" label="Name" />
        <% end %>

What is the best way to get the options to be the user.name? Is there AshPhoenix specific functionality for this, or is this something that i should just handle on my own?

Thanks.
Solution
Like fetching the possible users and assigning them on mount
Was this page helpful?