Ruby on Rails session and Data Marshalling

S

Summercool

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

Xavier Noria

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top