# Saving an array

**URL:** <https://www.ruby-forum.com/t/saving-an-array/89537>\
**Category:** Rails\
**Created:** [March 21, 2007, 1:39am UTC](https://www.ruby-forum.com/t/saving-an-array/89537 "2007-03-21T01:39:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 21, 2007, 1:39am UTC](https://www.ruby-forum.com/t/saving-an-array/89537/1 "2007-03-21T01:39:39Z")

</div>

Is there any way to save an array to a single column within a database?

I have an array of coordinates that are within an array.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 21, 2007, 2:28am UTC](https://www.ruby-forum.com/t/saving-an-array/89537/2 "2007-03-21T02:28:14Z")

</div>

> Is there any way to save an array to a single column within a database? I have an array of coordinates that are within an array.

you can flatten the array into a string via Marshal.dump (and use  
Marshal.restore to get it back). If you prefer to have “clearer”  
information in your db, then you could use to\_xml or to\_yaml, but it  
will take you a bit more to restore it than using Marshal.

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 21, 2007, 5:14am UTC](https://www.ruby-forum.com/t/saving-an-array/89537/3 "2007-03-21T05:14:24Z")

</div>

Alternatively, you could specify the column to be a serialized Array  
on your model. For example:

class Model \< ActiveRecord::Base  
serialize :coordinates, Array  
end

> > Model.create(:coordinates =\> [[1,2],[3,4]])  
> > …  
> > Model.find(:first, :order =\> ‘id DESC’).coordinates  
> > =\> [[1,2],[3,4]]

In your database, you’ll need to specify ‘coordinates’ as :text  
or :string (the former preferred if you have lots of data).

On Mar 21, 8:39 am, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 21, 2007, 9:50am UTC](https://www.ruby-forum.com/t/saving-an-array/89537/4 "2007-03-21T09:50:54Z")

</div>

This one likes like a solid plan. Thanks a lot.

Eden Li wrote:

> Alternatively, you could specify the column to be a serialized Array  
> on your model. For example:
> 
> class Model \< ActiveRecord::Base  
> serialize :coordinates, Array  
> end

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 21, 2007, 6:52am UTC](https://www.ruby-forum.com/t/saving-an-array/89537/5 "2007-03-21T06:52:10Z")

</div>

Thanks I will give it a shot.

javier ramirez wrote:

> > Is there any way to save an array to a single column within a database? I have an array of coordinates that are within an array.
> 
> you can flatten the array into a string via Marshal.dump (and use  
> Marshal.restore to get it back). If you prefer to have “clearer”  
> information in your db, then you could use to\_xml or to\_yaml, but it  
> will take you a bit more to restore it than using Marshal.
> 
> regards,
> 
> javier ramirez
> 
> ## –
> 
> Estamos de estreno… si necesitas llevar el control de tus gastos  
> visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
