storing as YAML in the database

6 views
Skip to first unread message

Josh

unread,
Aug 2, 2007, 2:07:46 PM8/2/07
to Ruby on Rails: Talk
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?

Jason Roelofs

unread,
Aug 2, 2007, 2:36:56 PM8/2/07
to rubyonra...@googlegroups.com
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

Josh

unread,
Aug 2, 2007, 2:53:27 PM8/2/07
to Ruby on Rails: Talk
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

On Aug 2, 1:36 pm, "Jason Roelofs" <jameskil...@gmail.com> wrote:
> 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
>

Jacob Atzen

unread,
Aug 2, 2007, 3:14:03 PM8/2/07
to rubyonra...@googlegroups.com
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 Atzen

Reply all
Reply to author
Forward
0 new messages