# Select helper and issues with :selected

**URL:** <https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362>\
**Category:** Rails\
**Created:** [March 19, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362 "2009-03-19T15:56:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/1 "2009-03-19T15:56:51Z")

</div>

I have this code:

\<%= select ‘stuff’, ‘id’, Stuff.find\_by\_sql( “SELECT stuffs.value,  
stuffs.id from stuffs WHERE account\_id = " + @account.id.to\_s + " ORDER  
BY stuffs.value ASC” ).collect{ |a| [a.value, a.id] }, {:include\_blank  
=\> true, :selected =\> @user.stuff\_profile.to\_s } %\>

The issue is is that the blank space is added but the value is not set  
as selected. I have this same code (only using a different table) and  
that code adds the blank space and selects the correct drop down item.  
When I view the html everything looks good - option has a value as the  
id and I am telling it to select using the id. As far as I can see this  
should work, but it doesn’t. Any ideas of what I am doing wrong. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/2 "2009-03-19T16:05:18Z")

</div>

On Mar 19, 2:56 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have this code:
> 
> \<%= select ‘stuff’, ‘id’, Stuff.find\_by\_sql( “SELECT stuffs.value,  
> stuffs.id from stuffs WHERE account\_id = " + @account.id.to\_s + " ORDER  
> BY stuffs.value ASC” ).collect{ |a| [a.value, a.id] }, {:include\_blank  
> =\> true, :selected =\> @user.stuff\_profile.to\_s } %\>

I’m not sure what stuff\_profile is, but the fact that you’re  
converting it, whatever it is, to a string means that it’s guaranteed  
not to be equal to any of those ids.

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/3 "2009-03-19T16:09:40Z")

</div>

Frederick C. wrote:

> I’m not sure what stuff\_profile is, but the fact that you’re  
> converting it, whatever it is, to a string means that it’s guaranteed  
> not to be equal to any of those ids.
> 
> Fred

I added that as a last ditch effort to try and make it work but with no  
luck. even without it it still doesn’t work.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/4 "2009-03-19T16:14:30Z")

</div>

Shandy N. wrote:

> I added that as a last ditch effort to try and make it work but with no  
> luck. even without it it still doesn’t work.

The stuff\_profile is just a field with an id in it would should match  
the value in the option - not well named, I know.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/5 "2009-03-19T16:20:03Z")

</div>

On 19 Mar 2009, at 15:09, Shandy N. wrote:

> luck. even without it it still doesn’t work.  
> what is stuff\_profile ? what ever you pass to :selected should be an  
> integer that will be == to the id of one of the instances of Stuff you  
> are returning

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/6 "2009-03-19T17:08:25Z")

</div>

I changed the find and still nada. Here is what I got now:

\<%= select( ‘stuff’, ‘id’, Stuff.find( :all, :conditions =\> [‘account\_id  
= ?’, @account.id.to\_s], :order =\> ‘value ASC’).collect{ |a| [a.value,  
a.id] }, { :include\_blank =\> true, :selected =\> @user.stuff\_profile })  
%\>

@user.stuff\_profile is an id and when I look at the html there is an  
option field with that id as it’s value, so it should find it and make  
it the selected option. I beginning to wonder if this is a bug in the  
helper?

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/7 "2009-03-19T17:29:59Z")

</div>

On Mar 19, 4:08 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I changed the find and still nada. Here is what I got now:
> 
> \<%= select( ‘stuff’, ‘id’, Stuff.find( :all, :conditions =\> [‘account\_id  
> = ?’, @account.id.to\_s], :order =\> ‘value ASC’).collect{ |a| [a.value,  
> a.id] }, { :include\_blank =\> true, :selected =\> @user.stuff\_profile })  
> %\>
> 
> @user.stuff\_profile is an id

Is it definitely an integer ? (ie “1” is != 1)

Fred  
and when I look at the html there is an

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 19, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/select-helper-and-issues-with-selected/163362/8 "2009-03-19T17:35:15Z")

</div>

Frederick C. wrote:

> On Mar 19, 4:08ï¿½pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I changed the find and still nada. Here is what I got now:
> > 
> > \<%= select( ‘stuff’, ‘id’, Stuff.find( :all, :conditions =\> [‘account\_id  
> > = ?’, @account.id.to\_s], :order =\> ‘value ASC’).collect{ |a| [a.value,  
> > a.id] }, { :include\_blank =\> true, :selected =\> @user.stuff\_profile })  
> > %\>
> > 
> > @user.stuff\_profile is an id
> 
> Is it definitely an integer ? (ie “1” is != 1)
> 
> Fred  
> and when I look at the html there is an

Nope, you are correct, I’m an idiot. The field was a chcracter varying  
field and when I added a to\_i method call it worked. Sorry Fred, I guess  
I did understand what you were getting at, in the future I’ll know  
better. Thanks for the help.

-S
