# Multiple Select Dropdown and Storing Problem

**URL:** <https://www.ruby-forum.com/t/multiple-select-dropdown-and-storing-problem/104440>\
**Category:** Ruby\
**Created:** [May 17, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/multiple-select-dropdown-and-storing-problem/104440 "2007-05-17T23:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [May 17, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/multiple-select-dropdown-and-storing-problem/104440/1 "2007-05-17T23:31:35Z")

</div>

I was working on this a couple of months back and could not figure  
out  
what I was doing wrong. I came back to it today and wanted to post  
again to see if any of you had an idea. Here is the problem.

I have this code:

 \<%= options\_from\_collection\_for\_select(@rflists, "id", "keyword", @selected) %\>

The field I am storing is called RepFor in the myrep table. The  
options for the dropdown comes from a keyword table that have an id  
and name. The options I want have been fed into rflists. This all  
works good and displays properly with a multiple select dropdown with  
the proper names and values. Here is the html it renders:

 Car Insurance Home Insurance Technology Accounting Legal

So if I select Car Insurance, Home Insurance and Technology and  
submit  
it this is what gets written to the DB. Exactly as shown below.

— - “1808” - “1809” - “1810”

When I reopen the form nothing is selected.

Now if I manually set @selected in my code as follows it selects the  
items I have forced when I reopen the form:

\<% @selected = 1808, 1809, 1810 %\>  
  
\<%= options\_from\_collection\_for\_select(@rflists, “id”, “keyword”,  
@selected) %\>

My question is what do I need to do to make the select write the data  
to the DB the same way I set it in @selected. Is there something  
that  
has to be done in the controller to keep it from being written as  
follows:

— - “1808” - “1809” - “1810”

or do I need to do something special when reading it back?

Thanks for your help.

Jim

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [May 18, 2007, 1:05am UTC](https://www.ruby-forum.com/t/multiple-select-dropdown-and-storing-problem/104440/2 "2007-05-18T01:05:50Z")

</div>

On May 17, 3:29 pm, Jim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was working on this a couple of months back and could not figure  
> out  
> what I was doing wrong. I came back to it today and wanted to post  
> again to see if any of you had an idea. Here is the problem.

…this looks like a Ruby on Rails question. You may have better  
success asking this question on the Ruby on Rails group, rather than  
the (general language) Ruby mailing list/newsgroup/forum beast.

[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [May 18, 2007, 7:02am UTC](https://www.ruby-forum.com/t/multiple-select-dropdown-and-storing-problem/104440/3 "2007-05-18T07:02:16Z")

</div>

On Fri, May 18, 2007 at 06:30:03AM +0900, Jim wrote:

> So if I select Car Insurance, Home Insurance and Technology and  
> submit  
> it this is what gets written to the DB. Exactly as shown below.
> 
> — - “1808” - “1809” - “1810”

That looks like YAML, but with some of the whitespace trimmed out.

irb(main):001:0\> require ‘yaml’  
=\> true  
irb(main):002:0\> YAML.load(%{— - “1808” - “1809” - “1810”})  
ArgumentError: syntax error on line 0, col 13: `--- - "1808" - "1809" - "1810"' from /usr/lib/ruby/1.8/yaml.rb:133:in`load’  
from /usr/lib/ruby/1.8/yaml.rb:133:in `load’  
from (irb):2  
irb(main):003:0\> YAML.load(%{—\n - “1808”\n - “1809”\n - “1810”})  
=\> [“1808”, “1809”, “1810”]

Note that what you’re trying to do is to put a quart into a pint pot -  
i.e.  
write a ruby Array into a single field in the database.
