# Polymorphic associations and forms

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-and-forms/143519>\
**Category:** Rails\
**Created:** [July 19, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-forms/143519 "2008-07-19T22:49:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![spike666](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@spike666](https://www.ruby-forum.com/u/spike666)\
**Post date:** [July 19, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-forms/143519/1 "2008-07-19T22:49:43Z")

</div>

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 field for choosing whether the sponsorship is for a  
Partner or Company and then I have 2 more 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).

Everything saves fine, but when I render the view, the Company and  
Partner fields aren’t set to the proper values. This is because  
they’re set to use :partner\_id and :company\_id rather than :sponsor\_id.

Is there any way of doing this? anyone know where I’m going wrong?

some sample code:

## the partial =\>

\<% fields\_for 'event[sponsor\_attributes][]', sponsor do |sf| -%\> Sponsor \<%= sf.select(:sponsor\_type, @sponsor\_types, { :prompt =\> 'Select a type...' }, :index =\> nil )%\> 
 \<%= label\_tag 'Candidate:' %\> \<%= sf.select(:company\_id, @companies, {:prompt =\> 'Choose a company...' }, :index =\> nil ) %\> 

 \<%= label\_tag 'Partner: ' %\> \<%= sf.select(:partner\_id, @partners, { :prompt =\> 'Choose a partner...' }, :index =\> nil ) %\> 

```
<% if sponsor.new_record? -%>
  <%= link_to_function '[-] Remove', "this.up('.sponsor').remove()"

```

%\>  
\<% else -%\>  
\<%= link\_to\_function ‘[-] Remove’, “mark\_for\_destroy(this,  
‘sponsor’)” %\>  
\<%= sf.hidden\_field :id, :index =\> nil %\>  
\<%= sf.hidden\_field :should\_destroy, :index =\> nil, :class =\>  
‘should\_destroy’ %\>  
\<% end -%\>

 \<% end -%\>

## the Event model’s important stuff (yes, I know… this is a mess) =\>

def sponsor\_attributes=(sponsor\_attributes)  
sponsor\_attributes.each do |attrib|  
unless attrib[:sponsor\_type].blank? # it’s just an empty field…  
if attrib[:id].blank? # it’s a new sponsorship!  
s = sponsorships.build()  
else # it’s an existing sponsorship  
s = sponsorships.detect { |sp| sp.id == attrib[:id].to\_i }  
end

```
    s.sponsor_type = attrib[:sponsor_type]

    # load the correct sponsor_id
    case attrib[:sponsor_type]
    when 'Company'
      s.sponsor_id = attrib[:company_id]
    when 'Partner'
      s.sponsor_id = attrib[:partner_id]
    end
  end
end

```

end

If I missed anything, let me know.

Thanks in advance.

…spike
