# Serializing an instance with arbitrary methods (in rails)

**URL:** <https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195>\
**Category:** Ruby\
**Created:** [November 17, 2005, 5:24pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195 "2005-11-17T17:24:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 5:24pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/1 "2005-11-17T17:24:11Z")

</div>

(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?

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. 🙂 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. 🙂

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 5:27pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/2 "2005-11-17T17:27:12Z")

</div>

This is supposed to be good stuff:

> **[YAML.rb: YAML for Ruby](https://sourceforge.net/projects/yaml4r/)**
>
> Download YAML.rb: YAML for Ruby for free. YAML.rb is a YAML processor for the Ruby language, coded in pure Ruby. Its implementation is simple and small, designed to be used in place of PStore.

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 5:57pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/3 "2005-11-17T17:57:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 6:06pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/4 "2005-11-17T18:06:23Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 6:24pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/5 "2005-11-17T18:24:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 6:39pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/6 "2005-11-17T18:39:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 6:24pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/7 "2005-11-17T18:24:33Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/439d5e/32.png) [@gavin](https://www.ruby-forum.com/u/gavin)\
**Post date:** [November 17, 2005, 8:46pm UTC](https://www.ruby-forum.com/t/serializing-an-instance-with-arbitrary-methods-in-rails/48195/8 "2005-11-17T20:46:55Z")

</div>

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

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
