AF
Ash Framework•4mo ago
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
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 %>
<.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
Jump to solution
3 Replies
ZachDaniel
ZachDaniel•4mo ago
It's something you'd handle on your own 🙂
Solution
ZachDaniel
ZachDaniel•4mo ago
Like fetching the possible users and assigning them on mount
srmico
srmicoOP•4mo ago
I figured 😄 Thanks for the help!

Did you find this page helpful?