Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve Marshal.dump "no marshal_dump is defined for class Proc" error

Suppose I have a User < ActiveRecord::Base. Now, suppose I would like to create a user instance, validate it and store it in the session if I can not save it.

user = User.new
...
session[:new_user] = user unless user.save
...

Using the ActiveRecord session one might end up with no marshal_dump is defined for class Proc coming from Marshal.dump.

Now, what is the best way to resolve such serialization errors (locate the "invalid" Proc objects) for an object (does not have to be AR in general) ?

like image 395
kares Avatar asked Nov 18 '25 22:11

kares


1 Answers

Usually it's better to store the user id in the session, not the User instance itself. This does mean you have to do a db (or memcache) lookup for authenticated requests, but it saves you a ton of headaches around serialization/caching that you'd otherwise have to deal with.

Also, it's not an "invalid" Proc in some sense. Proc instances are containers for Ruby code and application state and therefore cannot be serialized, ever.

like image 66
Brian Donovan Avatar answered Nov 21 '25 11:11

Brian Donovan