many to many relationships management.
I couldnt find clear way but got it working. Eventually i got it working, but i would like to get some tips/tricks, if this they way, or am i missing something obvious?
and in
Also just wanted to document this, so after a year when i need it agin, i can find it
# Resource
actions do
create :create do
primary? true
accept [:subject, :status, :matchers, :frequency]
argument :fetchers, {:array, :uuid}
change manage_relationship(:fetchers, type: :append_and_remove)
end
update :update do
primary? true
accept [:subject, :status, :matchers, :frequency]
argument :fetchers, {:array, :uuid}
change manage_relationship(:fetchers, type: :append_and_remove)
require_atomic? false
end
end
relationships do
many_to_many :fetchers, Scraper.Fetcher,
through: Scraper.TemplateFetcherRelationship,
source_attribute_on_join_resource: :template_id,
destination_attribute_on_join_resource: :fetcher_id
end# Resource
actions do
create :create do
primary? true
accept [:subject, :status, :matchers, :frequency]
argument :fetchers, {:array, :uuid}
change manage_relationship(:fetchers, type: :append_and_remove)
end
update :update do
primary? true
accept [:subject, :status, :matchers, :frequency]
argument :fetchers, {:array, :uuid}
change manage_relationship(:fetchers, type: :append_and_remove)
require_atomic? false
end
end
relationships do
many_to_many :fetchers, Scraper.Fetcher,
through: Scraper.TemplateFetcherRelationship,
source_attribute_on_join_resource: :template_id,
destination_attribute_on_join_resource: :fetcher_id
endand in
# Form component
defp assign_form(%{assigns: %{template: template}} = socket) do
prepared_context = context(socket, as: "template", forms: [auto?: true])
form =
if template do
template = template |> Ash.load!([:fetchers], context(socket))
AshPhoenix.Form.for_update(template, :update, [{:params, %{fetchers: Enum.map(template.fetchers, & &1.id)}} | prepared_context])
else
Knabis.Scraper.Template
|> AshPhoenix.Form.for_create(:create, [{:params, %{fetchers: []}} | prepared_context])
end
assign(socket, form: to_form(form))
end
<.input
field={@form[:fetchers]}
type="select"
label="Fetchers"
options={@fetcher_options}
multiple={true}
prompt="Select fetchers..."
description="Choose one or more fetchers to associate with this template"
/># Form component
defp assign_form(%{assigns: %{template: template}} = socket) do
prepared_context = context(socket, as: "template", forms: [auto?: true])
form =
if template do
template = template |> Ash.load!([:fetchers], context(socket))
AshPhoenix.Form.for_update(template, :update, [{:params, %{fetchers: Enum.map(template.fetchers, & &1.id)}} | prepared_context])
else
Knabis.Scraper.Template
|> AshPhoenix.Form.for_create(:create, [{:params, %{fetchers: []}} | prepared_context])
end
assign(socket, form: to_form(form))
end
<.input
field={@form[:fetchers]}
type="select"
label="Fetchers"
options={@fetcher_options}
multiple={true}
prompt="Select fetchers..."
description="Choose one or more fetchers to associate with this template"
/>Also just wanted to document this, so after a year when i need it agin, i can find it
