# Save Array

**URL:** <https://discuss.rubyonrails.org/t/save-array/62276>\
**Category:** rubyonrails-talk\
**Created:** [January 9, 2012, 12:02am UTC](https://discuss.rubyonrails.org/t/save-array/62276 "2012-01-09T00:02:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigo\_Ruiz](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@Rodrigo\_Ruiz](https://discuss.rubyonrails.org/u/Rodrigo_Ruiz)\
**Post date:** [January 9, 2012, 12:02am UTC](https://discuss.rubyonrails.org/t/save-array/62276/1 "2012-01-09T00:02:43Z")

</div>

Hi, is there a way to save an array of float values to my database (for a specific mode, for example User, so it stays like user.values = [0, 2, 54, 3])?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Ahmy\_Yulrizka](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@Ahmy\_Yulrizka](https://discuss.rubyonrails.org/u/Ahmy_Yulrizka)\
**Post date:** [January 9, 2012, 12:11am UTC](https://discuss.rubyonrails.org/t/save-array/62276/2 "2012-01-09T00:11:43Z")

</div>

Hmm, one way is to implement it as has\_many association.

but also you could save it as json or xml. or use serialization [http://api.rubyonrails.org/classes/ActiveModel/Serialization.html](http://api.rubyonrails.org/classes/ActiveModel/Serialization.html)

Ahmy Yulrizka

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson2](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dave\_Aronson2](https://discuss.rubyonrails.org/u/Dave_Aronson2)\
**Post date:** [January 9, 2012, 12:17am UTC](https://discuss.rubyonrails.org/t/save-array/62276/3 "2012-01-09T00:17:56Z")

</div>

There are lots of ways. For instance, if you really want to save it as an array, \*and you don't need to do any math on it\*, is to save it as a string, and turn it into an array again on reload.

However, the \*cleanest\* (since the above is no doubt making many people lose their lunch), most \*flexible\*, and probably even most \*easy to implement\* (since Rails would as usual do a lot of the work for you), would be to make another table. This table would include the ID(s) of the thing(s) the array is attached to, a positional index number (not to be confused with a database index), and the value.

For example if you're writing a web site for Immigration and Customs Enforcement, and you wanted to record what scores an immigrant has achieved on each of possibly many attempts to pass the citizenship test, you could use a table called TestAttempts, with immigrant\_id, try\_number, and score. This would be a pretty standard one-to-many relationship, where each Immigrant has\_many :test\_attempts, and each Test\_Attempt belongs\_to :immigrant. (Of course in such a scenario, you'd probably want more info on there like the date and place of the test, maybe a more detailed breakdown of the scoring, but that's another story.)

Or if you're setting up a more general web site, that could include data about \*many\* different tests, like a school, you could use TestAttempts with fields such as student\_id, test\_id, try\_number, and score. This would be a pretty standard \*many\*-to-many situation, where each Student has\_many :tests :through =\> :test\_attempts (and likewise many :scores), and similarly each Test has many :students the same way (and likewise many :scores).

-Dave

---

<div class="post-metadata">

**Author:** ![Paul8](https://avatars.discourse-cdn.com/v4/letter/p/858c86/32.png) [@Paul8](https://discuss.rubyonrails.org/u/Paul8)\
**Post date:** [January 9, 2012, 1:38am UTC](https://discuss.rubyonrails.org/t/save-array/62276/4 "2012-01-09T01:38:58Z")

</div>

But, if you knew that you'd never want to do any SQL actions on that array (it was just extra data attached to that record), wouldn't it execute faster if you just save it as a string? And as far as being clean, I'd think that not creating a table you didn't need was pretty clean, too.

(I ask, not because I am convinced that serializing the array is better, but because I keep wrestling with this every time it comes up.)

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson2](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dave\_Aronson2](https://discuss.rubyonrails.org/u/Dave_Aronson2)\
**Post date:** [January 9, 2012, 2:17am UTC](https://discuss.rubyonrails.org/t/save-array/62276/5 "2012-01-09T02:17:49Z")

</div>

> But, if you knew that you'd never want to do any SQL actions on that array

My crystal ball is rather foggy. Must need a good waxing. 😉

> wouldn't it execute faster if you just save it as a string?

Possibly... but the big question is, which is worth more, the few CPU milliseconds you save, or the hours of programmer-time to make any changes?

> And as far as being clean, I'd think that not creating a table you didn't need was pretty clean, too.

If you're getting overwhelmed with too many tables, chances are your app is too "enterprisey" anyway. 😉

-Dave

---

<div class="post-metadata">

**Author:** ![Christian\_Bautista](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@Christian\_Bautista](https://discuss.rubyonrails.org/u/Christian_Bautista)\
**Post date:** [January 9, 2012, 2:56am UTC](https://discuss.rubyonrails.org/t/save-array/62276/6 "2012-01-09T02:56:26Z")

</div>

Yes I think that is possible… after saving the array in the database trying playing arround with this codes…

.enum

.sym

Well, I have a doubt if that will work.

But there is nothing to lose if you try it…
