Saving application objects in your session will come back to haunt you

Posted About 13 years ago. Visible to the public.

If you save a non-standard object (not a String or Fixnum, etc) like the AwesomeClass from your application in the session of visitors be prepared that some time you will get this exception:

ActionController::SessionRestoreError: Session contains objects whose class definition isn't available. Remember to require the classes for all objects kept in the session. (Original exception: ...)

This happens when you remove your AwesomeClass but users come back to your site and still have the serialization of such objects in their session. Rails will then try to rebuild the user's session and raise an error out of stale_session_check! Show archive.org snapshot if it cannot find all classes.

The best way to go: Don't do this. Strings and numbers should be enough and if you think you need to store objects, think again.

If you messed up already and just want to avoid such errors you could resemble the model/class structure using an initializer. To be on the safe side you can also add initialize(*args) methods to these classes to eat up any arguments passed to them.

Arne Hartherz
Last edit
Over 11 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-04-21 17:18)