# Which select?

**URL:** <https://www.ruby-forum.com/t/which-select/111983>\
**Category:** Rails\
**Created:** [August 14, 2007, 1:25am UTC](https://www.ruby-forum.com/t/which-select/111983 "2007-08-14T01:25:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 14, 2007, 1:25am UTC](https://www.ruby-forum.com/t/which-select/111983/1 "2007-08-14T01:25:30Z")

</div>

Hi All,

I want to create a select list on a form with an array of dates from  
a DB similar to this.  
The date is used to search another DB for documents to edit.

2007-08-15 2007-08-22

Which if any of the selects would be best for this and  
allow what has been selected to be marked as selected  
until the choice is changed.

I have been using but never get the selected item to stick:

\<%= select\_tag(:pubdate, options\_for\_select  
(@pubdates, :selected=\>@pubdate)) %\>

Thanks for any hints/references/etc

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 14, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/which-select/111983/2 "2007-08-14T18:00:31Z")

</div>

Hi,

Tried it this morning but still won’t add the selected to the item  
choosen.

Thanks for the quick rely

Dave

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 14, 2007, 2:24am UTC](https://www.ruby-forum.com/t/which-select/111983/3 "2007-08-14T02:24:34Z")

</div>

leave out “:selected =\>” and it ought to work

\<%= select\_tag(:pubdate, options\_for\_select  
(@pubdates, @pubdate)) %\>

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 14, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/which-select/111983/4 "2007-08-14T20:08:48Z")

</div>

Hi,

I poked around and don’t know if it is a bug or feature ;-).

This works correctly:  
\<%= select\_tag(:mydate, options\_for\_select( %w{ 2007-08-22 2007-08-29  
2007-09-05 }, params[‘mydate’])) %\>  
Chosen item is selected when form is submitted

But this will not:

@pubdates is an array of dates from the DB same as above  
@pubdates = Pubdates.find(:all, :order=\>“pubdate”).map {|u|  
[u.pubdate] }

\<%= select\_tag(:pubdate, options\_for\_select(@pubdates, params  
[‘pubdate’])) %\>

Any insight into making this work is appreciated

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 15, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/which-select/111983/5 "2007-08-15T22:51:49Z")

</div>

Hi,

I was using a different name to put a second select for testing.

What I found was that ActiveRecord maps the list of dates to a  
different data type than the params that it is comparing to(this is a  
guess).  
What ended up working for me was casting what was returned from  
the DB to a string(I am a little green so I am not sure why this  
worked).

The solution was:  
Controller-  
@pubdates = Pubdates.find(:all, :order=\>“pubdate”).map {|u|  
[u.pubdate.to\_s] }  
View-  
\<%= select\_tag(:pubdate, options\_for\_select(@pubdates, params  
[‘pubdate’])) %\>

Now the chosen date is selected when the form is submitted and  
rendered again.

Thanks for pointing me in the right direction

Dave

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 15, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/which-select/111983/6 "2007-08-15T18:33:10Z")

</div>

> \<%= select\_tag(:pubdate, options\_for\_select(@pubdates, params  
> [‘pubdate’])) %\>
> 
> Any insight into making this work is appreciated

you’re using ‘mydate’ in the first example, but ‘pubdate’ in the  
second

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 16, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/which-select/111983/7 "2007-08-16T15:16:06Z")

</div>

> What I found was that ActiveRecord maps the list of dates to a  
> different data type than the params that it is comparing to(this is a  
> guess).  
> yes, if you store that field in the db as date or datetime, AR will  
> return it as a Date object. AR will return the column’s data cast to  
> the appropriate ruby type. i’m pretty sure all form params will be  
> strings by default.
