Forum: Ruby on Rails Saving an Array to MySQL... data changes...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brian C. (Guest)
on 2006-05-27 22:11
I have a multiple select menu:

<%= select_tag 'resume[employer_1_industry][]',
options_for_select(industries_helper, @resume.employer_1_industry),
:multiple=> true %>

It produces this HTML:


<select id="resume[employer_1_industry][]" multiple="multiple"
name="resume[employer_1_industry][]">
<option value="">- Select -</option>
<option value="1">Electrical Engineering - Analog</option>
<option value="2">Electrical Engineering - Digital</option>
<option value="3">Electrical Engineering - RF</option></select>

When I process the form the array from the multiple select produced is:

["1", "3"] or whatever the options are that I select. The problem seems
to be when I process the form. MySQL saves the array as:

---
- "1"
- "3"

I have the column set to 'text' datatype. I have tried everything from
forcing the column back to an array (.to_a) to forcing it to a string
before it is written to MySQL but nothing seems to work. The data can't
be read back into my app properly. Any help?
Brian C. (Guest)
on 2006-05-28 00:50
I found the answer... must serialize the column data in the model...


serialize  :employer_1_industry, Array



Brian C. wrote:
> I have a multiple select menu:
>
> <%= select_tag 'resume[employer_1_industry][]',
> options_for_select(industries_helper, @resume.employer_1_industry),
> :multiple=> true %>
>
> It produces this HTML:
>
>
> <select id="resume[employer_1_industry][]" multiple="multiple"
> name="resume[employer_1_industry][]">
> <option value="">- Select -</option>
> <option value="1">Electrical Engineering - Analog</option>
> <option value="2">Electrical Engineering - Digital</option>
> <option value="3">Electrical Engineering - RF</option></select>
>
> When I process the form the array from the multiple select produced is:
>
> ["1", "3"] or whatever the options are that I select. The problem seems
> to be when I process the form. MySQL saves the array as:
>
> ---
> - "1"
> - "3"
>
> I have the column set to 'text' datatype. I have tried everything from
> forcing the column back to an array (.to_a) to forcing it to a string
> before it is written to MySQL but nothing seems to work. The data can't
> be read back into my app properly. Any help?
unknown (Guest)
on 2006-05-28 13:10
(Received via mailing list)
I don't understand why your using an array. Why not use a relational
model, with an employers table and an industries table, then a habtm
join using an employers_industries table?
-N
Brian C. (Guest)
on 2006-06-04 21:20
I don't need a relational model here... just saving simple data.

unknown wrote:
> I don't understand why your using an array. Why not use a relational
> model, with an employers table and an industries table, then a habtm
> join using an employers_industries table?
> -N
This topic is locked and can not be replied to.