# Serialize object and store into database

**URL:** <https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827>\
**Category:** rubyonrails-talk\
**Created:** [April 10, 2008, 3:15pm UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827 "2008-04-10T15:15:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher1](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@Tarscher1](https://discuss.rubyonrails.org/u/Tarscher1)\
**Post date:** [April 10, 2008, 3:15pm UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827/1 "2008-04-10T15:15:55Z")

</div>

Hi all,

I want to serialize an object and store it into the database. This seems a trivial task but Google gives me no useful hints.

Can someone please give me a little help on what methods I can use to serialize and what type of column I best use to store serialized objects.

Many thanks Stijn

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 10, 2008, 3:49pm UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827/2 "2008-04-10T15:49:20Z")

</div>

> Hi all,
> 
> I want to serialize an object and store it into the database. This seems a trivial task but Google gives me no useful hints.
> 
> Can someone please give me a little help on what methods I can use to serialize and what type of column I best use to store serialized objects.

If you do class Foo \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;serialize :bar end

then the bar attribute will be serialized/deserialized as needed

Fred

---

<div class="post-metadata">

**Author:** ![Tarscher1](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@Tarscher1](https://discuss.rubyonrails.org/u/Tarscher1)\
**Post date:** [April 11, 2008, 8:37am UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827/3 "2008-04-11T08:37:35Z")

</div>

Thanks for the answer.

I can get the serialized object out of the database but it is now in YAML format. How can I convert this back into my vlass object?

Thanks

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 18, 2008, 1:28pm UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827/4 "2008-09-18T13:28:36Z")

</div>

Tarscher wrote:

> Thanks for the answer.
> 
> I can get the serialized object out of the database but it is now in YAML format. How can I convert this back into my vlass object?
> 
> Thanks
> 
> On Apr 10, 5:49�pm, Frederick Cheung \<frederick.che...@gmail.com\>

Look at the Marshal module. From the API docs:

&nbsp;&nbsp;&nbsp;&nbsp;class Klass &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(str) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@str = str &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def sayHello &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@str &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

(produces no output)

&nbsp;&nbsp;&nbsp;&nbsp;o = Klass.new("hello\n") &nbsp;&nbsp;&nbsp;&nbsp;data = Marshal.dump(o) &nbsp;&nbsp;&nbsp;&nbsp;obj = Marshal.load(data) &nbsp;&nbsp;&nbsp;&nbsp;obj.sayHello #=\> "hello\n"

---

<div class="post-metadata">

**Author:** ![salj](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@salj](https://discuss.rubyonrails.org/u/salj)\
**Post date:** [September 18, 2008, 2:00pm UTC](https://discuss.rubyonrails.org/t/serialize-object-and-store-into-database/27827/5 "2008-09-18T14:00:31Z")

</div>

Rails usually does the translation for you. All you need to do is access that attribute. If you saved it as an Array, then you should treat the attribute like any other array. The other way is to save it as a Hash and then you will treat it as a Hash when you access it.
