Serializing an instance with arbitrary methods (in rails)

(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 :slight_smile:

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?

Details:
I’m writing an app that’s an odd mixture of interactive fiction,
wiki, and MUD. (Well, single-user for now.) Think of a web version of
a “Choose Your Own Adventure” book, where anyone can write the pages/
choices, and every page and choice can have arbitrary ruby code
associated with it. Each page and choice is an ERB template (in the
future, with wiki markup), allowing the contents of the story and
text of the choices to vary.

Each page can run Ruby code against a ‘ReaderSession’ before the page
is viewed, setting the consequences of getting to the page. Each
choice can run Ruby code to decide if it may be shown (e.g. has the
user already made this choice? does the user have enough gold to buy
the rabbit? does the user have the sword needed to slay the dragon
here?). When the user makes a choice, code specific to that choice
can be run before the destination page is shown (e.g. subtract the
right amount of gold and add the rabbit to the inventory).

When the user starts a story, a blank ReaderSession object is
created. The code for each page/choice is run within the scope of
this instance, so that any instance variables may be set without
harming others (a sort of namespace) and I can easily start the story
over by clearing out the instance variables.

Certain details haven’t been ironed out yet: should each choice be
hardcoded to a destination page, or can the ‘result code’ for a
choice change which page you go to? How will I use AJAX to make this
a multi-user/reader game, so that when someone else arrives at the
same page, you know they’re there? Can pages have time limits that
force the user to make a choice before a default action is taken
automatically?

Thanks for any advice/brainstorming. :slight_smile: As with all my personal
projects, this will be an (advertisement-free), free service for
the .net to enjoy. (In other words, helping me isn’t helping with
some commercial project you’ll never see. :slight_smile:

This is supposed to be good stuff:

On Nov 17, 2005, at 10:21 AM, Gavin K. wrote:

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?

What’s wrong with just sticking the object in the session?

James Edward G. II

James Edward G. II wrote:

What’s wrong with just sticking the object in the session?

Erps, did I forget to include that? I’d like a user to be able to
return to the story after quitting/restarting/exploding the computer,
and resume with the ReaderSession where s/he left off. Unless I’m
missing an option in Rails, sessions go away when the session ends.
(The cookie identifier is set to expire when the browser closes.)

On Thursday 17 November 2005 9:21 am, Gavin K. wrote:

best way?
Use Marshal for this. YAML offers similar capabilities, but it is MUCH
slower, doesn’t handle circular references and such as well, and
historically
has had bizarre corner cases pop up that broke it. It may be that those
corner cases are all resolved, but unless you need a human readable
serialization format, use Marshal.

Kirk H.

On Thursday 17 November 2005 11:22, Kirk H. wrote:

Use Marshal for this. YAML offers similar capabilities, but it is MUCH
slower, doesn’t handle circular references and such as well, and
historically has had bizarre corner cases pop up that broke it. It may be
that those corner cases are all resolved, but unless you need a human
readable serialization format, use Marshal.

Or unless you’re also using DRb on the same objects and need to have
them
DRbUndumped.

Unless I’m missing an option in Rails,
sessions go away when the session ends.
(The cookie identifier is set to expire when the browser closes.)

Yeah, apparently I was missing something. Or at least, further
searching is showing me words that appear to be related to what I want
to do.

Sorry for the noise. RTFM and all that.

On Nov 17, 2005, at 8:21 AM, Gavin K. wrote:

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 Z.
Yakima Herald-Republic
WebMaster

509-577-7732
[email protected]