Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Storing a Hash in a database table using YAML--anything better?

Reply
Thread Tools

Storing a Hash in a database table using YAML--anything better?

 
 
J. B. Rainsberger
Guest
Posts: n/a
 
      10-19-2006
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!
--
J. B. (Joe) Rainsberger :: http://www.jbrains.ca
Your guide to software craftsmanship
JUnit Recipes: Practical Methods for Programmer Testing
2005 Gordon Pask Award for contribution Agile Software Practice

 
Reply With Quote
 
 
 
 
Gregory Seidman
Guest
Posts: n/a
 
      10-20-2006
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...-with-sti.html

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


 
Reply With Quote
 
 
 
 
J. B. Rainsberger
Guest
Posts: n/a
 
      10-20-2006
Gregory Seidman wrote:
> 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...-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.
--
J. B. (Joe) Rainsberger :: http://www.jbrains.ca
Your guide to software craftsmanship
JUnit Recipes: Practical Methods for Programmer Testing
2005 Gordon Pask Award for contribution Agile Software Practice

 
Reply With Quote
 
David Vallner
Guest
Posts: n/a
 
      10-20-2006
--------------enig704D5B2FE891C78364F35FC6
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

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 t=

o
> 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.
>=20


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 Vallner


--------------enig704D5B2FE891C78364F35FC6
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFOKCNy6MhrS8astoRAvnRAJ9XWxWNifFw2JBYfAI3xF b9lJ71qgCfWkd9
x0bBEMZ9UhagLEweL+9nOFY=
=bKTY
-----END PGP SIGNATURE-----

--------------enig704D5B2FE891C78364F35FC6--

 
Reply With Quote
 
Bil Kleb
Guest
Posts: n/a
 
      10-22-2006
J. B. Rainsberger wrote:
> I'm a beginning Rails programmer, so please be gentle.


Hi JB! Welcome.

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing a hash of a hash in a DBM database Colvin Perl Misc 3 12-30-2003 04:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57