# Storing as YAML in the database

**URL:** <https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115>\
**Category:** Rails\
**Created:** [August 2, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115 "2007-08-02T20:09:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 2, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115/1 "2007-08-02T20:09:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 2, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115/2 "2007-08-02T20:38:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 2, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115/3 "2007-08-02T21:01:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 2, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/storing-as-yaml-in-the-database/111115/4 "2007-08-02T21:15:12Z")

</div>

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.
