adding data to the junction table
How can i add data to a junction/pivot table through a form?
I have create action enabled on the junction table
21 Replies
AshPhoenix has some great tooling around that. Please read the docs carefully, because AshPhoenix Forms function a bit differently than Ecto changesets. https://hexdocs.pm/ash_phoenix/AshPhoenix.Form.html#module-working-with-related-data
I did read it but i couldn't get much out of it. That is why i came to support channel
Could you expand a little bit? What did you try, what part of it is confusing specifically?
You'll want to look at these docs as well: https://hexdocs.pm/ash/Ash.Changeset.html#manage_relationship/4
Before you try to do it with a form, get it working by calling actions
Is it something like
cast_assoc
?Yes
It works similarly
but there are a lot more options/instructions
is there any example in ash-hq?
Nope
For instance, one of your options might be something like this:
That would create a destination record, and a join record, passing certain options to the join resource action
I have three tables at the moment. Teams, Users, Team Joined User(junction table)
many_to_many relation is defined in teams and users. this is what i have done so far.
for this implementation.
What i was trying to achieve here was to have a dropdown of user that gets selected and then have a hidden input of
team_id
and insert it in the junction table
but it gives me this in form submit_errors: [team: {"is required", []}, user: {"is required", []}]
Do i need to define the form like this instead of normal create one for teamjoineduser?
I am not getting it π
Have you read the entirety of the documentation about
manage_relationship
yeah i am reading it as we talk.
In a nutshell, you're going to create a custom action that 1) accepts args for the relationships, and 2) configure
manage_relationship
for that action. Then you can use that custom action in the form.
Then you can use forms: [auto?: true]
and it will be a lot simpler, IMO.@frankdugan3 action will be defined like we defined
code_interface
, right?This is not a many to many, just an example of the kind of thing you'd do:
okay, i think i get it a little.
it will be defined in my junction table resource, right?
Then, that gives AshPhoenix the info it needs to do this instead:
Much easier to grep.
I will try it in the morning. Thank you.
in the above action i defined it for create and in here you said
Form.for_update
?
Defined this in team joined user resource.
now it works but two insertions are going at same time
I think I am missing something here.
@Zach Daniel just wondering which extension you are using for autocomplete in ASH resource?
P.s. I just saw your twitter video and you showed the many_to_many relation through the ash admin π
I used a different approach for this. After looking at the twitter repo by @Zach Daniel . I made my junction table attributes attribute_writable? true
and then added this create action:
and now it works for me.π€ you just wanted to be able to create an instance of the join table? TBH I still donβt really understand exactly what you were trying to do, but Iβm glad you got it worked out π
basically, i had three tables
1. users
2. teams
3. team joined user (junction table)
I wanted to make an addition to junction table through the form.
And did you get it worked out with
[auto?: true]
forms?yes it is working now