tags:

views:

225

answers:

3

I have a table which should store an id, a name and a hash. How do I serialize the Hash? (I'm using Ruby and Sequel as ORM.)

+2  A: 

If you're using Sequel::Model, the Serialization plugin should do the trick.

Greg Campbell
It works! Thanks a lot. I probably should have spent a little bit more time reading the docs: http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/Serialization.html
t6d
A: 

Nor Ruby nor SQL are ORM. You can use ActiveRecord from Rails. You will have to define a class with attributes you specified and than simply use save and find methods.

Gabriel Ščerbák
Sequel is an ORM (more precisely, a "database toolkit" with an ORM built on top of it). See http:://sequel.rubyforge.org/ for more information.
Greg Campbell
+2  A: 

You seem to have found a sufficient answer but I thought I'd chip in with a general-purpose ruby way.

# outside of rails you'll need this
require 'base64'

# encode
h = { :first => "John", :age => 23 }
encoded = Base64.encode64(Marshal.dump(h))

# decode
h = Marshal.load(Base64.decode64(encoded))

I use this to serialize ruby objects (e.g. across JSON and to the DB), and you'll find that cookie sessions in Rails encode the session hash in the same way. It's often handy to debug the session contents from a browser cookie using this.

The Sequel serialization plugin works the same way. You can decide if you want the use Marshal, Yaml or Json for serializing and deserializing your data. But thanks for the tip. ;)
t6d