# Views/Controllers for polymorphic association

**URL:** <https://www.ruby-forum.com/t/views-controllers-for-polymorphic-association/119328>\
**Category:** Rails\
**Created:** [November 6, 2007, 2:22am UTC](https://www.ruby-forum.com/t/views-controllers-for-polymorphic-association/119328 "2007-11-06T02:22:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![osman84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/82dd89/32.png) [@osman84](https://www.ruby-forum.com/u/osman84)\
**Post date:** [November 6, 2007, 2:22am UTC](https://www.ruby-forum.com/t/views-controllers-for-polymorphic-association/119328/1 "2007-11-06T02:22:28Z")

</div>

Hello,  
I’m wondering what a good way is to create forms for polymorphic  
associations (or what syntax I can use). A simple example should  
suffice.

For example, I’ve got models that look like:

Tournament —\> belongs\_to :place, :polymorphic =\> true  
Country --\> has\_many :tournaments, :as=\> :place  
Continent --\> has\_many :tournaments, :as=\> :place

I’m more concerned about the belongs\_to part of the association, ie what  
the form code for Tournament should look like. I want the UI to have a  
select list, where the user can choose a country or a continent (can be  
in the same list or separate lists, doesn’t matter). Then the  
controller should be able to create/update the tournament by setting  
place correctly.

Thanks!

---

<div class="post-metadata">

**Author:** ![osman84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/82dd89/32.png) [@osman84](https://www.ruby-forum.com/u/osman84)\
**Post date:** [November 6, 2007, 5:24am UTC](https://www.ruby-forum.com/t/views-controllers-for-polymorphic-association/119328/2 "2007-11-06T05:24:22Z")

</div>

Osman,

I’ve just been revisiting this issue for a project. I’m a relative  
noob so my approach may not be the “correct” one. I’ve created a  
plugin called acts\_as\_polymorph that:

Proxies the attributes from the polymorph table into the attribute  
table  
Overwrites the new, save and update methods to synchronise updates to  
both tables in a transaction  
Implements find\_poly\_\* methods via method\_missing that scopes the  
finds and :includes the polymorph table

With this approach you can:

1. Treat all attributes of the polymorph table as if they are part of  
the attribute table(s)
2. Do normal new, create, save, updates and ensure tables are  
synchronised
3. Have extended finders that include the polymorph table  
automatically

For forms, this means you just treat the attributes from the polymorph  
table as if  
they are in the attribute table - no special handling requried.

My code isn’t finished (no tests, not implemented destroy, …) but  
its working well enough to suggest this might be a reasonable  
strategy. Happy to share the code  
as-is if you want, just email me.

Cheers --Kip

On Nov 6, 9:22 am, Osman O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
