# Can you unserialize an object from the db?

**URL:** <https://www.ruby-forum.com/t/can-you-unserialize-an-object-from-the-db/53140>\
**Category:** Rails\
**Created:** [December 12, 2005, 4:45pm UTC](https://www.ruby-forum.com/t/can-you-unserialize-an-object-from-the-db/53140 "2005-12-12T16:45:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 12, 2005, 4:45pm UTC](https://www.ruby-forum.com/t/can-you-unserialize-an-object-from-the-db/53140/1 "2005-12-12T16:45:37Z")

</div>

I don’t even know if this is possible. I want to use an object as an  
attribute for one of my classes. I can save it to the db fine, looks  
like…but I need to be able to use it after I pull the object from  
the db. Right now the attribute is just a String, but I need it to be  
a Runt::Intersect object. Does anyone know how I can unserialize the  
attribute so I can use it as an object?

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 12, 2005, 4:48pm UTC](https://www.ruby-forum.com/t/can-you-unserialize-an-object-from-the-db/53140/2 "2005-12-12T16:48:39Z")

</div>

On 12/12/05, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t even know if this is possible. I want to use an object as an  
> attribute for one of my classes. I can save it to the db fine, looks  
> like…but I need to be able to use it after I pull the object from  
> the db. Right now the attribute is just a String, but I need it to be  
> a Runt::Intersect object. Does anyone know how I can unserialize the  
> attribute so I can use it as an object?

Check out ActiveRecord::Base.serialize:  
[http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000731](http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000731)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 12, 2005, 6:43pm UTC](https://www.ruby-forum.com/t/can-you-unserialize-an-object-from-the-db/53140/3 "2005-12-12T18:43:04Z")

</div>

It’s not really clear to me what you’re trying to do, but another option  
is  
to use Marshal::dump and Marshal::load to store a binary representation  
of  
the object into a BLOB column. That’s probably not a good idea if you  
know  
what type of object you want to store (if you know what the object looks  
like, you should probably store it in the normal ActiveRecord way.) On  
the  
other hand, using Marshal lets you store any object type- it’s more  
flexible  
but less clear, harder to support, and probably slower.
