# Saving an Array to MySQL... data changes

**URL:** <https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807>\
**Category:** Rails\
**Created:** [May 27, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807 "2006-05-27T20:11:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 27, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807/1 "2006-05-27T20:11:30Z")

</div>

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 - Electrical Engineering - Analog Electrical Engineering - Digital Electrical Engineering - RF

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?

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 27, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807/2 "2006-05-27T22:50:19Z")

</div>

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 - Electrical Engineering - Analog Electrical Engineering - Digital Electrical Engineering - RF
> 
> 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?

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [May 28, 2006, 11:10am UTC](https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807/3 "2006-05-28T11:10:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [June 4, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/saving-an-array-to-mysql-data-changes/61807/4 "2006-06-04T19:20:16Z")

</div>

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
