# Store hash in a DB column

**URL:** <https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677>\
**Category:** Rails\
**Created:** [June 30, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677 "2006-06-30T18:31:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [June 30, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/1 "2006-06-30T18:31:12Z")

</div>

I’m just wondering. Is there an easy way to take a hash that I have  
and store it in a DB text column and then read it as a hash from it  
again.

The reason is that I have a db table called contents. But it stores  
all kinds of information and even data that I do not know of yet.

I know I could store it inside the text column using XML or YAML or  
something else but then I would have to write some sort of parser to  
change the data between the two. So I was hoping that there was some  
way of simply storing the hash without conversion in the text field  
and get it back as a hash.

–

---

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [June 30, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/2 "2006-06-30T19:04:44Z")

</div>

> then just have a free\_form\_data column in your db table and AR will  
> take care of serializing and deserializing your hash or other objects.

What is the datatype of the column?

---

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [June 30, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/3 "2006-06-30T18:59:42Z")

</div>

On Jun 30, 2006, at 9:30 AM, Jon Gretar B. wrote:

> way of simply storing the hash without conversion in the text field  
> and get it back as a hash.
> 
> ## –
> 
> Jon Gretar B.  
> [http://www.jongretar.net/](http://www.jongretar.net/)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

class MyModel \< AR::Base

serialize :free\_form\_data

end

then just have a free\_form\_data column in your db table and AR will  
take care of serializing and deserializing your hash or other objects.

-Ezra

---

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [June 30, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/4 "2006-06-30T19:15:09Z")

</div>

On Jun 30, 2006, at 10:04 AM, Jon wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

just use :text

-Ezra

---

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [December 13, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/5 "2007-12-13T12:57:48Z")

</div>

I tried the ways above but not working. Please help.’

JÃ³n BorgÃ¾Ã³rsson wrote:

> On 6/30/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class MyModel \< AR::Base
> > 
> > serialize :free\_form\_data
> > 
> > end
> > 
> > then just have a free\_form\_data column in your db table and AR will  
> > take care of serializing and deserializing your hash or other objects.
> 
> ## Excellent. I’ll try that.

---

<div class="post-metadata">

**Author:** ![jongretar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jongretar](https://www.ruby-forum.com/u/jongretar)\
**Post date:** [June 30, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/store-hash-in-a-db-column/96677/6 "2006-06-30T19:51:52Z")

</div>

On 6/30/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class MyModel \< AR::Base
> 
> serialize :free\_form\_data
> 
> end
> 
> then just have a free\_form\_data column in your db table and AR will  
> take care of serializing and deserializing your hash or other objects.

## Excellent. I’ll try that.
