Ruby on Rails session and Data Marshalling

It seems that Ruby on Rails session is done by using Data Marshalling
(or also known as Data Serialization or Data Deflating)…

So let’s say if my session has a class Foo with an array of Bar
objects…

so when the user access each new page (GET or POST to the Rails
server), then the session data is regenerated from the textfile or
database table session…

Now in that case, any class variable in the Bar class can be wrong…
because let’s say the Bar class has the class variable of total or
count, but Rails is not re-generating them… Rails is only re-
generating the Bar objects. No Initialize() was ever called… in a
way, it is like the objects are cloned… without ever “initialized”.

On Sep 11, 2007, at 7:35 AM, Summercool wrote:

Now in that case, any class variable in the Bar class can be wrong…
because let’s say the Bar class has the class variable of total or
count, but Rails is not re-generating them… Rails is only re-
generating the Bar objects. No Initialize() was ever called… in a
way, it is like the objects are cloned… without ever “initialized”.

In general it is bad practice to store objects in the session, the
definition of their classes may change, you need to sync them if they
are stored in the database and changes are done to different
instances… Because of the gotchas of marshalling you are
discovering, usually you store in session only simple, non-
application objects lik numbers, strings, etc. As per stuff from the
database you typically store just database IDs and fetch whatever
necessary.

I guess the question was posted to ruby-talk because it was about
marshalling, but since the motivation was web sessions I thought the
reply would warn about marshalling in that context in particular.

– fxn