Sterling Rose Design Blog

Dynamic form with a many-to-many relationship

1 Comments
Tags: Javascript Rails associations

My customer’s application is an in-house ordering system (among other things). The order form has a drop-down list to select the client placing the order. It also has two other drop-down lists: primary_contact_id and secondary_contact_id, both of which are based on the Contact model. My customer is certain he will only ever need to associate two contacts with an order, so I’m comfortable with this approach.

The sticking point was that I only want to populate the primary and secondary contact select boxes with contacts that are associated with a particular client. Client and Contact have a many-to-many association.

Ryan Bates’ Dynamic Select Menus Railscast got me most of the way there, but his example (countries/states) was one-to-many, not many-to-many. Following was my adaptation:

views/javascripts/dynamic_contacts.js.erb

var contacts =...
Read the whole post...


Copyright 2007-2010, Sterling Rose Design. All rights reserved.