Spike Grobstein | 19 Jul 22:48

Polymorphic associations and forms


Hi,

I've got an issue where I'm creating a form in my view where the user
can add a Sponsorship to an Event (m2m relationship using has_many
:through). A Sponsorship belongs to an event and also belongs to a
sponsor. The sponsor is a polymorphic relationship with either a Partner
or a Company. So I have these columns set up:

Event
  id
  name
  date

Sponsorship
  event_id
  sponsor_type
  sponsor_id

I created a view, much like in railscasts 3-part complex forms tutorial
where I can dynamically add and remove sponsorships for the event, and
there's a <select> field for choosing whether the sponsorship is for a
Partner or Company and then I have 2 more <select> fields, one with a
list of Partners and one with a list of Companies. I set up virtual
attributes in the Event model  for :company_id and :partner_id and I
have logic in that model as well which wires everything up the best I
can (because of the way this is working, I couldn't figure out how to
make everything work like magic as in the railscast, but I did get that
working for a non-polymorphic relationship elsewhere in the project).

(Continue reading)


Gmane