Serializing objects to the database

I have a rails app that has a search mechanism with a bunch of
options. I’ve consolidated all the search parameters into a
SearchQuery class. Now I want to store searches to be recalled later.
I searched the wiki but couldn’t find any pages about how to do this
best. I’m worried that if I just serialize the object I won’t be able
to change the class without breaking the searches that are already in
the database.

How flexible is the unserializing to changes? Should I just implement
my own (yaml based) serializing/unserializing that I can then version
to handle upgrades or is that overkill?


Should work fine as long as the property names stay the same.

you could easily try this for yourself in irb.

irb(main):013:0> class MyThing
irb(main):014:1> def initialize( a, b )
irb(main):015:2> @a, @b = a, b
irb(main):016:2> end
irb(main):017:1> end
=> nil

irb(main):018:0> thing = 1, 2 )
=> #<MyThing:0x2aaaab4f44e8 @b=2, @a=1>

… then inspect the serialized data:

irb(main):023:0> serialized = Marshal.dump( thing )
=> “\004\010o:\fMyThing\a:\[email protected]\a:\[email protected]\006”

try messing around with the class on the fly, and reload the marshalled

irb(main):042:0> thing2 = Marshal.load( serialized )
=> #<MyThing:0x2aaaab4f7990 @b=2, @a=1>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs