AshPhoenix nested forms not rendering existing data

When showing a form for some resource’s update action, how can I show the existing data on render.

    form =
      id
      |> AdvisorKey.get_by_id!(load: :users_join_assoc)
      |> AshPhoenix.Form.for_update(:update,
        as: "advisor_key",
        api: NaviClient.Clients,
        forms: [auto?: true]
      )
      |> to_form()


    update :update do
      accept [:name]

      argument :users_join_assoc, {:array, :map} do
        allow_nil? true
      end

      change manage_relationship(:users_join_assoc,
               on_no_match: :create,
               on_match: :ignore,
               on_missing: :destroy
             )
    end

  relationships do
    many_to_many :users, NaviClient.Accounts.User do
      through NaviClient.Clients.AdvisorKeyUser
      source_attribute_on_join_resource :advisor_key_id
      destination_attribute_on_join_resource :user_id
    end
  end

This is how I create the form, when I access the form value like form[:users_join_assoc].value the expected non empty list of values is present. However when I render the form with inputs_for/1, no content is rendered. Manually setting the nested form and setting the data option had no effect either. This test seems to do what I want, but I can’t replicate the behaviour.

:thinking: That configuration won’t do anything with the existing data because the on_match logic is to :ignore it. What should happen if the user modifies fields on the existing relationship? I guess you want to show it so that they can remove it? Try adding on_match: :update and see if that fixes it. If so, there may be something we should modify about the decision internally to use or not use the existing relationship when rendering forms. i.e if on_match :ignore but there is on_missing configured, we should probably use the existing related data.

That did the trick. I didn’t think update was what I wanted because its just a join table comprised of a compound primary key but all tests pass now. Thank again again for the quick help

Could you open a bug for this in ash_phoenix? I believe what is happening here is that, if there is on_missing we should show the original data in forms even if there is no on_match behavior.

Sure thing I’ll post an issue for this and the other thread once I have some time tomorrow

1 Like