# How can I "select" a model to add to my has\_many relationship?

**URL:** <https://www.ruby-forum.com/t/how-can-i-select-a-model-to-add-to-my-has-many-relationship/135763>\
**Category:** Rails\
**Created:** [April 24, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/how-can-i-select-a-model-to-add-to-my-has-many-relationship/135763 "2008-04-24T19:19:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 24, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/how-can-i-select-a-model-to-add-to-my-has-many-relationship/135763/1 "2008-04-24T19:19:41Z")

</div>

Here’s the situation …

Client has\_many :licenses, :as =\> :holdable  
Vendor has\_many :licenses, :as =\> :holdable

License belongs\_to :holdable, :polymorphic =\> true  
License belongs\_to :asset

So when creating/editing an asset, I also need to create/edit the  
licenses associated with that asset. I need an intuitive way to allow  
the user to select from a list of possible “holders” and assign one to  
a license. My idea was to have some sort of AJAX “suggest” type field  
where the user would type in the name of the holder and you get the  
right object back.

Anyways, I’m having a hard time coming up with an elegant way to  
populate the asset’s holdable property using AJAX. Any ideas on how I  
could pass the info during a create/update?

TIA,

Sean

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 24, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/how-can-i-select-a-model-to-add-to-my-has-many-relationship/135763/2 "2008-04-24T19:57:20Z")

</div>

Wow! That is a great solution. I’ve seen that guys screencasts a few  
other times but they’ve fallen off my radar lately. Thanks for the  
great suggestion.

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [April 24, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/how-can-i-select-a-model-to-add-to-my-has-many-relationship/135763/3 "2008-04-24T19:29:43Z")

</div>

This Railscasts episode might give you some ideas on how to achieve  
that:

> **[\#102 Auto-Complete Association - RailsCasts](http://railscasts.com/episodes/102-auto-complete-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
