# Select method

**URL:** <https://www.ruby-forum.com/t/select-method/93696>\
**Category:** Rails\
**Created:** [April 6, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/select-method/93696 "2006-04-06T19:18:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/select-method/93696/1 "2006-04-06T19:18:56Z")

</div>

The documentation states as an example:  
select(“post”, “person\_id”, Person.find\_all.collect {|p| [ p.name, p.id  
] },  
{ :include\_blank =\> true }) could become:

 David Sam Tobias

I have the following code in a \_form.rhtml file:  
\<%= select(“category”, “prnt\_id”, Prnt.find\_all.collect {|p| [p.name,  
p.id]},  
{ :selected =\> ‘prnt\_id’ == @category.prnt\_id, :include\_blank =\> false }  
) %\>

The drop down is created with the correct content, but is does NOT  
select the  
currently set value. The value is set if I use any other type of input  
(ie  
text\_field).

Is there an error in the doc? If so, does anybody know how ‘selected’  
is set?

TIA  
g

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/select-method/93696/2 "2006-04-06T19:30:36Z")

</div>

Gerard wrote:

> I have the following code in a \_form.rhtml file:  
> \<%= select(“category”, “prnt\_id”, Prnt.find\_all.collect {|p| [p.name,  
> p.id]},  
> { :selected =\> ‘prnt\_id’ == @category.prnt\_id, :include\_blank =\> false }  
> ) %\>

Hey Gerard,

From the api:  
By default, post.person\_id is the selected option. Specify :selected =\>  
value to use a different selection or :selected =\> nil to leave all  
options unselected.

Instead of :selected =\> value, you’ve got :selected =\> ‘prnt\_id’ ==  
@category.prnt\_id

You should probably try this:

{ :selected =\> @category.prnt\_id, :include\_blank =\> false }

Dan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/select-method/93696/3 "2006-04-06T19:24:59Z")

</div>

> currently set value. The value is set if I use any other type of input  
> (ie  
> text\_field).
> 
> Is there an error in the doc? If so, does anybody know how ‘selected’ is  
> set?

Try it without the :selected option. I use this all over the palce and  
that  
is the only difference I see between yours and mine. Rails will call @  
category.prnt\_id for you in the select helper. Also just as an FYI,  
:include\_blank =\> false is the default setting.

\<%= select(“category”, “prnt\_id”, Prnt.find\_all.collect {|p| [p.name,  
p.id]}  
) %\>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/select-method/93696/4 "2006-04-06T21:59:05Z")

</div>

I am also using rails 1.1. Check to make sure that @category.prnt\_id is  
returning what you expect it should be returning. It may be returning  
nil,  
or a value which doesnt exist in the options for the select.

mark

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 6, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/select-method/93696/5 "2006-04-06T21:34:42Z")

</div>

Mark Van H. \<mvette13@…\> writes:

> I have the following code in a \_form.rhtml file:  
> \< \<%= select(“category”, “prnt\_id”,  
> :include\_blank =\> false is the default setting.
> 
> \<%= select(“category”, “prnt\_id”, Prnt.find\_all.collect {|p| [  
> p.name, p.id]} ) %\>-- Mark

Mark,

I have tried it that way too, and the way Dan Perez suggested after you,  
but  
still no joy:  
\<%= select(“category”, “prnt\_id”,  
Prnt.find\_all.collect {|p| [p.name, p.id] })  
{ :include\_blank =\> false } ) %\>  
or  
\<%= select(“category”, “prnt\_id”,  
Prnt.find\_all.collect {|p| [p.name,p.id]}))%\>  
or Dan’s. All do not seem to generate a selected= in the html code.

I’m using Rails 1.1. How about you?

g

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 1, 2006, 2:27am UTC](https://www.ruby-forum.com/t/select-method/93696/6 "2006-12-01T02:27:19Z")

</div>

Mark Van H. wrote:

> I am also using rails 1.1. Check to make sure that @category.prnt\_id is  
> returning what you expect it should be returning. It may be returning  
> nil,  
> or a value which doesnt exist in the options for the select.
> 
> mark

I’m having the same issue as Gerard, using Rails 1.1.6. I cannot get it  
to select an option by default (or by specifying value to select).  
Here’s my code:

\<%= select(“thing”, “category\_id”, Category.find\_all.collect { |c|  
[c.name, c.id] }) %\>

Yes, @thing.category\_id is what I’m expecting and it is a value in the  
options.

Does this work at all?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 1, 2006, 5:53am UTC](https://www.ruby-forum.com/t/select-method/93696/7 "2006-12-01T05:53:38Z")

</div>

Daniel wrote:

> Mark Van H. wrote:
> 
> > I am also using rails 1.1. Check to make sure that @category.prnt\_id is  
> > returning what you expect it should be returning. It may be returning  
> > nil,  
> > or a value which doesnt exist in the options for the select.
> > 
> > mark
> 
> I’m having the same issue as Gerard, using Rails 1.1.6. I cannot get it  
> to select an option by default (or by specifying value to select).  
> Here’s my code:
> 
> \<%= select(“thing”, “category\_id”, Category.find\_all.collect { |c|  
> [c.name, c.id] }) %\>
> 
> Yes, @thing.category\_id is what I’m expecting and it is a value in the  
> options.
> 
> Does this work at all?

I think I found the problem, actually. It looks like @thing.category\_id  
is being treated as a String while c.id is being treated as a Fixnum.  
Annoying.
