How to have a selector which lists a another table's filtered records to choose from

forms
ecto
phoenix

#1

Hi, I’ve got two schema - Parent & Student. These have many-to-many associations between them.
I’m developing a CRUD app using phoenix. I’ve already created a form to create new Parents and it’s working well.

I wish to provide another form which will enable the user to create a Student record. However, from within this form (or a pop-up modal form), I wish to have the something that allows me to click-select and associate the Student record I’m creating with one/two existing Parent records.
Could someone please suggest a facility within Phoenix/Ecto that readily provides this, if it exists? Or what would you recommend as best practice for such a use case.

Any help would be greatly appreciated.
Thanks.


#2

Welcome to the community.

I don’t think phoenix have out of the box many to many like this.

What I’ve done is in situation like this, within the context of your problem, is to have a query to get all students. The controller will pass this list of students to your view and template. It is up to your form template and view to provide a select input for students either multiple or single or whatever with that student list.

Ecto is able to insert via multi or whatever other solution.

I usually make these type of query, listing all students, into a plug since it comes up often enough across controllers.


#3

Thank you so much. I will try this out.