# Select list for Pragmatic multiple models one form tutorial

**URL:** <https://www.ruby-forum.com/t/select-list-for-pragmatic-multiple-models-one-form-tutorial/140019>\
**Category:** Rails\
**Created:** [June 10, 2008, 4:56am UTC](https://www.ruby-forum.com/t/select-list-for-pragmatic-multiple-models-one-form-tutorial/140019 "2008-06-10T04:56:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@Jeff\_Lee](https://www.ruby-forum.com/u/Jeff_Lee)\
**Post date:** [June 10, 2008, 4:56am UTC](https://www.ruby-forum.com/t/select-list-for-pragmatic-multiple-models-one-form-tutorial/140019/1 "2008-06-10T04:56:22Z")

</div>

After going through the multiple models one form tutorial at Pragmatic  
([http://media.pragprog.com/titles/fr\_arr/multiple\_models\_one\_form.pdf](http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf))  
and the one from Rails casts, I’ve chosen to use the one at Pragmatic  
for its simplicity. Everything is easy to understand and works pretty  
well; however, I’m having some trouble with select lists.

The pertinent code is below:

\_email.html.erb:

 \<% new\_or\_existing = email.new\_record? ? 'new' : 'existing' %\> \<% prefix = "profile[#{new\_or\_existing}\_email\_attributes][]" %\> \<% fields\_for prefix, email do |email\_form| -%\> 

Email: \<%= email\_form.text\_field :address %\> Device: \<%= collection\_select (:email, :device\_id, Device.find(:all), :id, :name, options ={:prompt =\> "Select a device"}, html\_options ={:class =\> "device", :name =\> "profile[#{new\_or\_existing}\_email\_attributes][][device\_id]"}) %\> \<%= link\_to\_function "remove" , "$(this).up('.email').remove()" %\>

 \<% end -%\>

The fields in this form are given names and ids such that they either  
have the index and new or existing:

Email: Device: Select a device Fax Mobile Office Home remove

As you may notice, I just stuck the name in for the device\_id select  
field. When I do this, it does not preselect the existing value (this  
is a huge pain). The tutorial suggests the following for a simple  
select:

\<%= task\_form.select :completed, [[‘No’ , false], [‘Yes’ , true]] %\>  
which would be something like this in my variation:  
\<%= email\_form.select :device\_id, [[‘No’ , false], [‘Yes’ , true]] %\>

But, I do not know how I can provide my select options to this using a  
find. When I include this suggested code the name and id are  
appropriate and the selected item is correct. This is obviously not  
the ideal solution as it defeats the purpose of having my options in a  
table (they do need to be dynamic).

As a side note, at this point in time (deadlines) the application  
needs to be finished. Thus, I am unable to keep “improving” the code  
with newer and better methods or ways of doing things. While I would  
enjoy other options to develop multiple models in one form and may  
implement them at a later date, I would sincerely appreciate ways to  
solve this select issue while maintaining the rest of the code (which  
is close to the tutorial).

Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![Jeff\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@Jeff\_Lee](https://www.ruby-forum.com/u/Jeff_Lee)\
**Post date:** [June 10, 2008, 7:49am UTC](https://www.ruby-forum.com/t/select-list-for-pragmatic-multiple-models-one-form-tutorial/140019/2 "2008-06-10T07:49:11Z")

</div>

I’ve figured out how to accomplish what I’ve asked so I thought it  
might be useful for others. To generate the select list as the  
tutorial would want you would use the select method:

\<%= email\_form.select :device\_id, Device.find(:all).collect {|p|  
[p.name, p.id] }, { :prompt =\> “Select a device” } %\>
