Select list for Pragmatic multiple models one form tutorial

After going through the multiple models one form tutorial at Pragmatic
(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!

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” } %>