matt-savvy
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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
taskafter you do that? Do you have policies that could be filtering the reads oncontextsetc.?matt-savvy
Nope, no policies here. The
contextsandcontext_relationshipsare indeed being loaded.This is where I’m at right now:
The only thing I’m seeing that doesn’t seem quite right is the
relationshipsandargumentsfield of the ash changeset. I’m not sure ifargumentsshould have an empty list:zachdaniel
Ah, I think it might be the
value_is_keysetting? Can you try removing that?matt-savvy
I think that is what’s behind it. I’ve updated my action, removing
value_is_key. I also had to update the type:Now the
field.valueis a list ofPhoenix.HTML.Formstructs with data for each of the existingTodoTaskContexts (the resource we’re joining through).Is this a bug in creating the form? Or is it just that using
value_is_keymeans that it’s expectingcontext_relationshipsto be a list of those ids insteat of the actual structs?I’m using
value_is_keybecause the form is just iterating over the existingContexts that are available and creating a checkbox group, so the data that’s being submitted is a list of the ids. If there’s a way to tweak that so it plays nice with the way Ash handles forms like this, I’m all ears.zachdaniel
Yeah, this seems like a bug of some kind to me. Could you create a reproduction and open an issue on
ash_phoenix?matt-savvy
Will do.
Edit: done. Added here with a link to a GH repo reproducing it.
https://github.com/ash-project/ash_phoenix/issues/384
matt-savvy
Looking at your response in GH, it does seem like adding that option
auto?: [include_non_map_types?: truedoes at least handle the specific issue offield.valuealways being empty.That being said, even with those values present, I’m still having an issue getting this wired up.
And in the markup itself:
This is submitting correctly, I can see in my logs the
contextswith the ids.The issue I’m still having is when the form is rendered, the relationships that currently exist in the values aren’t showing up as selected.
I’m starting to think I might just be about this in the wrong way. I feel like this should be a fairly common form pattern with a many-to-many relationship like this. Like updating a
Teamto have multipleUsers. Is there an obvious approach here that I’m missing?I’m still fairly new to Ash so the help is much appreciated.
zachdaniel
That doesn’t look like enough to make those things show up as selected to me.
ahhh, okay. So what is happening here is that the argument itself doesn’t contain a value that corresponds to what is currently selected. The value of
@form[:contexts]is now a list of nested forms not a list value? Honestly its been a while since I looked at this part of the code, sorryTry doing something like this:
matt-savvy
Yup, the value of
@form[:contexts]is a list ofPhoenix.HTML.Form:So I can map the input value like this
or even hard-code it like so
value={[1, 2]}, but when it comes time to submit the form, it triggers this warningmatt-savvy
So it seems like there’s two things that might not be working together correctly.
What’s interesting is that I can hard code the
valuelikevalue={[1,2]}and submit it fine, and all I change is adding the nested form config, and then I get theGot non-map paramswarning again.