I have a resource TodoTask
that has a many-to-many with a Context
like so
# in todo_task
relationships do
has_many :context_relationships, TodoTaskContext do
destination_attribute :task_id
end
many_to_many :contexts, Context do
join_relationship :context_relationships
source_attribute_on_join_resource :task_id
destination_attribute_on_join_resource :context_id
end
end
I’m trying to set up the form and change
so that all existing Contexts
are listed as checkboxes, and when the form is submitted, there will be a TodoTaskContext
created for this TodoTask
and Context
. I have this working correctly in a unit test with a manage_relationship
that looks like this:
# in todo_task
update :update do
accept ...
argument :contexts, {:array, :string}, allow_nil?: true, default: []
require_atomic? false
change manage_relationship(:contexts, :context_relationships,
value_is_key: :context_id,
type: :direct_control
)
end
and I’m creating a form in my LV like so
AshPhoenix.Form.for_update(task, :update, as: "task")
But the related contexts aren’t being loaded. When I log out my @form[:contexts]
, the value is always an empty list.
%{
id: "task_contexts",
name: "task[contexts]",
value: [],
__struct__: Phoenix.HTML.FormField,
field: :contexts,
errors: []
}
I’ve made sure my relationships are loaded just before creating AshPhoenix.Form.for_update
, but that doesn’t seem to change anything:
task = task |> Ash.load!([:contexts, :context_relationships])
Is there something wrong with how I’ve got my manage_relationship
set up? Or is there something else I need to add?
Thanks!