Multi Select Drop Down Save Data to mysql

We are working on a database and need the capabilities to have a drop
down field (list) that can handle multiple selections. For example, if
it say to me please enter your favorite cars from this list and wanted
to use a list instead of a checkbox, it is possible. However, we
cannot figure out how to store the data in the database, retrieve the
values from the database and the type of field to choose. We know in
PHP this is called "form array data" but the code for ruby seems
limited. Anyone know where we can find code for ruby on rails to do
this? Thank you in advance!

-David

David wrote:

We are working on a database and need the
capabilities to have a drop down field (list) that
can handle multiple selections.

WRT the type of field to use to collect the selections, see the documentation for select_tag at http://api.rubyonrails.org/ . Can't figure out what it is you're stuck on wrt to the other items.

hth,
Bill

This is the html code and the backend is a ruby on rails server
connecting to mysql. We need to use a selection box to give a list of
options that you can choose multiple entries and submit the form. It
should store the multientry field so it can be retrieved and show
selected when editing the data again. Sorry if this is confusing but I
am not sure hwo to explain it.

<select name="myrep[RepFor][]" multiple="multiple" id="myrep_RepFor">

  <option value="0">Select One</option>

   <% @rflists.each do |keyword| %>

   <option value="<%= keyword.id %>"

   <%= ' selected' if keyword.id == @myrep.RepFor %>> <%=
keyword.keyword %> </option>

  <% end %>

</select>

We need to use a selection box to give a list of
options that you can choose multiple entries and
submit the form.

select_tag will get you this.

It should store the multientry field so it can be retrieved

Without knowing more about your model, it's not possible to offer any constructive advice.

and show selected when editing the data again.

I haven't tried it, but I'd be willing to bet select_tag will do this too inasmuchas it takes a string for the options.

hth,
Bill

Bill is right:

select_tag( 'myrep', @rflists, :multiple => true )

OR

select("myrep", "repfor", @rflists.collect {|k| [ k.keyword, k.id ] } )

** Untested code from a rails newbie....

thank you...will try this tonight when I get home and will update you
after. Thanks again for the quick replies.

just curious, should the mysql field be char or int?

Once we figure this out the 2nd part will be to perform a join from the
information stored in this field back to our keyword list table and
return the text values. We have a table with a coupld hundred keyword
values that are used throughout the system so it will need to join back
to show in the list. It always the stupid little things like this that
get us stuck.

thanks again

david

Rails expects things to be structured a certain way in order to perform
its magic (convention over configuration).

If the foreign key is named after the table it refers to with a
trailing "_id", then rails can do all the joining for you as described
by your models. Specifics are a little tricky without the schema but
check out
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530
for clues.

So we go it to store the values or so we think...this is what we see in
the varchar field...is that the right data type for the field? Should
we do int? Those numbers correspond to our keyword table and the key
from that table.

--- - "1830" - "1832" - "1833"

using this code:

<% @selected = @myrep.RepFor.collect { |m| m.id.to_i } %>

<%= select_tag ('myrep[RepFor][]',
options_from_collection_for_select(@rflists,"id","keyword", @selected),
:multiple => true ) %>

however, we are not sure how to get it to show the values that were
selected when going into the edit form. I have to say we have written
so many things in ruby and this has been by far the most frustrating
(due to lack of doc).

All the examples I have seen use int as the data type - probably best
to stick with it.

One thing I have learnt so far with ruby - if it is difficult you are
doing something wrong. How about this?

options_for_select(@rflists, @myrep.RepFor)

* Not sure of how you arrays are constructed, but it should be this
simple....

it not difficult...the code just does not seem to work as it should.
Your code might work if we were using a regular list. However, our
values come from another table that hold our keywords. I wish I could
just show you and then it would make sense.

Unfortunately the documentation for rails is poor, which does not help
newbies like myself, or even experienced developers like DHH (see
http://coderpath.libsyn.com/index.php?post_id=160139). We need
documentation like Django's.

Throw a few dollars at http://blog.caboo.se/pages/documentation_drive
to help out...

David wrote:

it not difficult...the code just does not seem to work as it should.
Your code might work if we were using a regular list. However, our
values come from another table that hold our keywords. I wish I could
just show you and then it would make sense.

Ahh - now I see the reasons - the arrays do not line up like I thought.
Are they related via foreign keys? If so you will be able to build a
single array that sticks it all together, then use a variation of the
code above.

It is based on a foreign key. The table field myrep contains the
foreign key relation from the keyword table. The numbers 1830, 1832
and 1833 (seen above) are the id of the entries in the keyword table.
We normally write sql statements to join them together (for single
instance drop down boxes). However, in this case we cannot figure out
how to pull apart the string array stored in the varchar field. If you
could pull it apart I am pretty sure we could do some sql stuff to join
the names in the list. Do you know how to parse the string array
stored in the varchar field?

It is so strange when it comes to ruby 99% of the things are so easy
but this one has us in a tail spin. I have been writing in ruby on
rails for about a year now and the lack of doc has made it a bit
difficult but not hard....but it is getting there between this google
site the api site.

Thanks again for your help!

How about:

keywords = "1 2 3 1 2 3 4 5"
keywords.split
=> ["1", "2", "3", "1", "2", "3", "4", "5"]
keywords.split.uniq
=>["1", "2", "3", "4", "5"]
keywords.split.uniq.each do |keyword|
  # do fancy stuff here
end