# Setting a default option for a select list of a new object

**URL:** <https://www.ruby-forum.com/t/setting-a-default-option-for-a-select-list-of-a-new-object/104600>\
**Category:** Rails\
**Created:** [May 20, 2007, 8:46am UTC](https://www.ruby-forum.com/t/setting-a-default-option-for-a-select-list-of-a-new-object/104600 "2007-05-20T08:46:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [May 20, 2007, 8:46am UTC](https://www.ruby-forum.com/t/setting-a-default-option-for-a-select-list-of-a-new-object/104600/1 "2007-05-20T08:46:01Z")

</div>

Yet another Rails newby getting his feet wet…

I have a lookup table for states in a database that I use to create a  
select list for objects with addresses.

When creating a new object (a business, in this case), I want the  
“State” select field to automatically have, say, Idaho selected. How  
do I do that? Here’s my code so far:

# Business Controller

def new  
@business = Business.new  
@states = State.find(:all) # so the \_form partial can  
create the select list  
end

# Business Model

belongs\_to :state #set up the relationship between Businesses  
and States

# \_form partial

\<%= select\_tag(‘state\_id’, options\_for\_select(@states.collect {|s|  
[s.name, s.id]}, @business.state\_id)) %\>

The @business.state\_id will select the state for a business that  
already exists when this \_form partial is used in the edit view. I  
want to have a default state selected (Idaho) for NEW businesses that  
isn’t the first one in the list (Alabama).

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [May 20, 2007, 10:00am UTC](https://www.ruby-forum.com/t/setting-a-default-option-for-a-select-list-of-a-new-object/104600/2 "2007-05-20T10:00:08Z")

</div>

Hi,

an obvious answer to this would be to have one variable collecting the  
State object for Idaho, and one collecting all states. To make it  
more dynamic, maybe it’s better to leave the state as a session  
variable, or a global variable, or something stored in an object  
expressing local preferences …

@local = State.find\_by\_name(“Idaho”)  
@states = State.find(:all)

then, in the form  
\<%=  
options = [[@local.name],[@local.id]] + @states.collect{|s|  
[s.name,s.id]}  
select(“state”,“id”,options)  
%\>  
(you could maybe add a separator between the local state and the full  
list of state  
[["--------",""]] or similar)

Elise

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [May 21, 2007, 3:15am UTC](https://www.ruby-forum.com/t/setting-a-default-option-for-a-select-list-of-a-new-object/104600/3 "2007-05-21T03:15:52Z")

</div>

I’m wondering if there’s a way to use the initialize method of the  
BusinessModel to achieve the desired result…
