# Storing a Hash in a database table using YAML--anything bett

**URL:** <https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952>\
**Category:** Ruby\
**Created:** [October 20, 2006, 1:52am UTC](https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952 "2006-10-20T01:52:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [October 20, 2006, 1:52am UTC](https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952/1 "2006-10-20T01:52:09Z")

</div>

I’m a beginning Rails programmer, so please be gentle.

I want to store a hash as a single row in a database table. I’m  
currently storing each name/value pair as a row with a reference back to  
the “parent” row, but that seems unnecessarily complex. I want to know  
whether you have any better suggestions than what I plan to try to do.

I plan to write the hash into the row in a single column by YAMLing it.  
I would, of course, unYAML on read operations.

I do not plan ever to need to run a query on a single key in this hash,  
so I feel comfortable storing it blob-like this way. I just wanted to  
know whether anyone knows of a better way than YAML formatting/parsing  
in my Rails model object.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [October 20, 2006, 3:20am UTC](https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952/2 "2006-10-20T03:20:19Z")

</div>

On Fri, Oct 20, 2006 at 08:51:47AM +0900, J. B. Rainsberger wrote:  
} I’m a beginning Rails programmer, so please be gentle.  
}  
} I want to store a hash as a single row in a database table. I’m  
} currently storing each name/value pair as a row with a reference back  
to  
} the “parent” row, but that seems unnecessarily complex. I want to know  
} whether you have any better suggestions than what I plan to try to do.  
}  
} I plan to write the hash into the row in a single column by YAMLing  
it.  
} I would, of course, unYAML on read operations.  
}  
} I do not plan ever to need to run a query on a single key in this  
hash,  
} so I feel comfortable storing it blob-like this way. I just wanted to  
} know whether anyone knows of a better way than YAML formatting/parsing  
} in my Rails model object.

Look at ActiveRecord::Base.serialize. It lets you declare a column (make  
sure it’s a TEXT column) to be a serialized Ruby object. You can store  
your  
hash automatically. You may also be interested in this example use of  
it:  
[http://redcorundum.blogspot.com/2006/07/ror-additional-attributes-with-sti.html](http://redcorundum.blogspot.com/2006/07/ror-additional-attributes-with-sti.html)

} Thanks for your help!  
} J. B. (Joe) Rainsberger :: [http://www.jbrains.ca](http://www.jbrains.ca)  
–Greg

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [October 20, 2006, 12:12pm UTC](https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952/3 "2006-10-20T12:12:08Z")

</div>

J. B. Rainsberger wrote:

> I want to store a hash as a single row in a database table. I’m  
> currently storing each name/value pair as a row with a reference back to  
> the “parent” row, but that seems unnecessarily complex. I want to know  
> whether you have any better suggestions than what I plan to try to do.

Disclaimer: A lot of the following will be database access performance  
related. In personal code with low usage, those can be safely ignored,  
but for a self-education project, I prefer following good practices to  
learn them even if they seem clunky.

What’s the structure of the hash? If it could be replaced by a data  
structure, I’d do so and keep this approach (it IS the correct one from  
the relational data modelling point of view), unless you’re sure the  
hash is an atomic value object by design.

I’d especially not use the serialization approach if you need to filter  
your data according to the contents of that hash - the “draw all records  
from the DB and filter them in Ruby” approach makes baby DBA Jesus cry  
and is a good candidate for becoming a bottleneck. I’d also not use the  
serialization approach if it’s binary serialisation - if you ever had to  
access the database from anything except Rails (e.g. Crystal Reports),  
you’re screwed.

David V.

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [October 20, 2006, 6:24am UTC](https://www.ruby-forum.com/t/storing-a-hash-in-a-database-table-using-yaml-anything-bett/72952/4 "2006-10-20T06:24:41Z")

</div>

Gregory S. wrote:

> }  
> } I do not plan ever to need to run a query on a single key in this hash,  
> } so I feel comfortable storing it blob-like this way. I just wanted to  
> } know whether anyone knows of a better way than YAML formatting/parsing  
> } in my Rails model object.
> 
> Look at ActiveRecord::Base.serialize. It lets you declare a column (make  
> sure it’s a TEXT column) to be a serialized Ruby object. You can store your  
> hash automatically. You may also be interested in this example use of it:  
> [Ruby, iOS, and Other Development: RoR: Additional Attributes with STI](http://redcorundum.blogspot.com/2006/07/ror-additional-attributes-with-sti.html)

This is exactly the kind of thing I was hoping to see. Thank you, Greg!  
I’ll let you know how it goes.
