# How can I specify the selected value in collection\_select wh

**URL:** <https://www.ruby-forum.com/t/how-can-i-specify-the-selected-value-in-collection-select-wh/103674>\
**Category:** Rails\
**Created:** [May 9, 2007, 5:39am UTC](https://www.ruby-forum.com/t/how-can-i-specify-the-selected-value-in-collection-select-wh/103674 "2007-05-09T05:39:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [May 9, 2007, 5:39am UTC](https://www.ruby-forum.com/t/how-can-i-specify-the-selected-value-in-collection-select-wh/103674/1 "2007-05-09T05:39:30Z")

</div>

Hi all,  
I have a relatively simple scenario. I have a form that can have a  
list of multiple phone objects in it. I’m trying to get the edit and  
entry screens to work with unique field names. I have entry and  
record creation working, but I’m still working on edit. Is it  
possible for me to specify the value of the selected index to the  
“collection\_select” method via an option or html option? I read the  
doc, but I can’t find what I’m looking for. Below is my code, as you  
can see my field and select names are “phone\_#” where # is the  
iteration index, but the actual variable is “phone”.

Main form snippet

```auto
<h2>Phones</h2>
<table>
<%
n = 0
for phone in @phones do
fieldname = "phone_#{n}";
%>
<tr><td>Number:</td><td> <%= text_field_tag fieldname, phone.number%></
td> <td><%= get_phone_types fieldname%></td></tr>
<%
n += 1
end
%>
<table>
```

helper with get\_phone\_types

```auto
 #
  #Create a Phone type select List
  #Take a list of html options
  #
  def get_phone_types(fieldname)
    @phones = PhoneType.find(:all, :order => "description ASC");
    #TODO, how to assign the value from phone.phone_type.id?
    collection_select(fieldname, "phone_type_id",
@phones, :id, :description);
  end
```

Also, is it possible for me to call the PhoneType.find and save the  
resulting array “@phones” for the life of the request across multiple  
method calls? I’m assuming I just need to make @phones an instance  
variable, but I’m new to ROR, and I’m unsure of the best practices for  
caching domain data for selects.

Thanks,  
Todd
