# Serialize object and store into database

**URL:** <https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417>\
**Category:** Rails\
**Created:** [April 10, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417 "2008-04-10T17:16:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 10, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417/1 "2008-04-10T17:16:38Z")

</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:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 10, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417/2 "2008-04-10T17:50:46Z")

</div>

On 10 Apr 2008, at 16:15, Tarscher wrote:

> 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  
serialize :bar  
end

then the bar attribute will be serialized/deserialized as needed

Fred

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [September 18, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417/3 "2008-09-18T15: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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Look at the Marshal module. From the API docs:

```
class Klass
  def initialize(str)
    @str = str
  end
  def sayHello
    @str
  end
end

```

(produces no output)

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

```

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 11, 2008, 10:38am UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417/4 "2008-04-11T10:38:37Z")

</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

On Apr 10, 5:49 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [September 18, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/serialize-object-and-store-into-database/134417/5 "2008-09-18T16:01:14Z")

</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.

On Sep 18, 8:28 am, Guy Van den berg \<rails-mailing-l…@andreas-
