From: Ezra Zygmuntowicz Date: 2005-11-18T04:43:58+09:00 Subject: Re: Serializing an instance with arbitrary methods (in rails) On Nov 17, 2005, at 8:21 AM, Gavin Kistner wrote: > (I know there's a rails mailing list. For some reason this feels > more appropriate to ruby-talk. I'll go to the other ML if this one > fails :) > > Summary: > I'm writing a rails app where I want to save information about a > user's session, so that they may return at a later date and pick up > where they left off. The information being saved is an object with > arbitrary instance variables. What's the best way (or any good way) > to save this? So far, the only thing I can think of is Marshal dump/ > load to a db row, but this seems off for some reason. Is this the > best way? > > > (-, /\ \/ / /\/ > > Gavin- There is a macro in rails for serializing an object into one column in your db table. It uses yaml and once you set it it will automatically serialize and unserialize when you save/retrieve it form the db. I think this is what you want. It works like so: In your model you define which column to serialize and what type of object to re-hydrate after retrieving it from the db: class MyModel < ActiveRecord::Base serialize :notes, :class_name => "Hash" end And thats all there is to it. HTH- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com