Storing as YAML in the database


#1

i’ve got a project which i need to create some dynamic forms. each one
will be different so it seems almost impossible to keep a 1 to 1
relationship between each form field and a corresponding column in the
database.

i’ve heard that YAML is a lot faster than XML. would this be something
i can use or is there something else that anyone would recommend for
something like this?


#2

Aye.

class MyThingy < AR::Base
serialize :form_data
end

The form_data field will now be automatically saved as YAML text on
save/create, and pulled back out into valid Ruby objects.

For example:

MyThingy.create :form_data => {:one => 1, :two => 2}

becomes

one: 1
two: 2

Jason


#3

oh man, that’s perfect! i looked up serialize in the api and it says
that it will deserialize it when it is loaded. does that mean that
i’ll also be able to do this?

my_thingy.form_data.one
my_thingy.form_data.two


#4

Josh wrote:

oh man, that’s perfect! i looked up serialize in the api and it says
that it will deserialize it when it is loaded. does that mean that
i’ll also be able to do this?

my_thingy.form_data.one
my_thingy.form_data.two

No, it would be my_thingy.form_data[:one], but that’s close enough.


Cheers,

  • Jacob A.