# Model object in session problems

**URL:** <https://rubytalk.org/t/model-object-in-session-problems/17183>\
**Category:** ruby-talk\
**Created:** [26 March 2005 10:12 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183 "2005-03-26T10:12:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob\_Lally](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Rob\_Lally](https://rubytalk.org/u/Rob_Lally)\
**Post date:** [26 March 2005 10:12 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183/1 "2005-03-26T10:12:50Z")

</div>

Hi,

I'm having a problem storing an AR model object in a session. Actually the problem doesn't seem to be related to storing it in a session. The problem occurs when the session is deserialised on the next request.

When the user logs in I pop a User object (an AR model) into the session and redirect to the homepage. Then I see the following error

\<ActionController::SessionRestoreError: Session contained objects where the class definition wasn't available. Remember to require classes for all objects kept in the session. The session has been deleted. (Original exception: undefined class/module User [ArgumentError])\>

Can anyone shed any light on my problem?

Thanks,

R.

---

<div class="post-metadata">

**Author:** ![Rob\_Lally](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Rob\_Lally](https://rubytalk.org/u/Rob_Lally)\
**Post date:** [26 March 2005 16:52 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183/2 "2005-03-26T16:52:17Z")

</div>

Sorry - just realised I sent this to the ruby list rather than the rails list.

R.

Rob Lally wrote:

> **···**
>
> > Hi,
> > 
> > I'm having a problem storing an AR model object in a session. Actually the problem doesn't seem to be related to storing it in a session. The problem occurs when the session is deserialised on the next request.
> > 
> > When the user logs in I pop a User object (an AR model) into the session and redirect to the homepage. Then I see the following error
> > 
> > \<ActionController::SessionRestoreError: Session contained objects where the class definition wasn't available. Remember to require classes for all objects kept in the session. The session has been deleted. (Original exception: undefined class/module User [ArgumentError])\>
> > 
> > Can anyone shed any light on my problem?
> > 
> > Thanks,
> > 
> > R.

---

<div class="post-metadata">

**Author:** ![Rob\_Lally](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Rob\_Lally](https://rubytalk.org/u/Rob_Lally)\
**Post date:** [26 March 2005 17:31 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183/3 "2005-03-26T17:31:09Z")

</div>

Thanks - that solves the problem.

I don't understand why it is necessary though. I assumed that the application had already required the User class and the user file since this is reading in something it just created and wrote out. What am I missing?

R.

Moses Hohman wrote:

> **···**
>
> > If you "require 'user'" in your application.rb (so that it will be required in every controller), the problem should go away. The problem is, as I understand it, that the homepage controller does not automatically know to require 'user'.
> > 
> > On Mar 26, 2005, at 10:52 AM, Rob Lally wrote:
> > 
> > > Sorry - just realised I sent this to the ruby list rather than the rails list.
> > > 
> > > R.
> > > 
> > > Rob Lally wrote:
> > > 
> > > > Hi,  
> > > > I'm having a problem storing an AR model object in a session. Actually the problem doesn't seem to be related to storing it in a session. The problem occurs when the session is deserialised on the next request.  
> > > > When the user logs in I pop a User object (an AR model) into the session and redirect to the homepage. Then I see the following error  
> > > > \<ActionController::SessionRestoreError: Session contained objects where the class definition wasn't available. Remember to require classes for all objects kept in the session. The session has been deleted. (Original exception: undefined class/module User [ArgumentError])\>  
> > > > Can anyone shed any light on my problem?  
> > > > Thanks,  
> > > > R.

---

<div class="post-metadata">

**Author:** ![Moses\_Hohman](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Moses\_Hohman](https://rubytalk.org/u/Moses_Hohman)\
**Post date:** [26 March 2005 17:36 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183/4 "2005-03-26T17:36:33Z")

</div>

I assume that your login controller and your homepage controller are different classes? In that case, while your login controller may automatically require your user class definition, your homepage controller won't. If that doesn't make any sense, then I'm misunderstanding your situation and I'd have to see your code.

M

> **···**
>
> On Mar 26, 2005, at 11:31 AM, Rob Lally wrote:
> 
> > Thanks - that solves the problem.
> > 
> > I don't understand why it is necessary though. I assumed that the application had already required the User class and the user file since this is reading in something it just created and wrote out. What am I missing?
> > 
> > R.
> > 
> > Moses Hohman wrote:
> > 
> > > If you "require 'user'" in your application.rb (so that it will be required in every controller), the problem should go away. The problem is, as I understand it, that the homepage controller does not automatically know to require 'user'.  
> > > On Mar 26, 2005, at 10:52 AM, Rob Lally wrote:
> > > 
> > > > Sorry - just realised I sent this to the ruby list rather than the rails list.
> > > > 
> > > > R.
> > > > 
> > > > Rob Lally wrote:
> > > > 
> > > > > Hi,  
> > > > > I'm having a problem storing an AR model object in a session. Actually the problem doesn't seem to be related to storing it in a session. The problem occurs when the session is deserialised on the next request.  
> > > > > When the user logs in I pop a User object (an AR model) into the session and redirect to the homepage. Then I see the following error  
> > > > > \<ActionController::SessionRestoreError: Session contained objects where the class definition wasn't available. Remember to require classes for all objects kept in the session. The session has been deleted. (Original exception: undefined class/module User [ArgumentError])\>  
> > > > > Can anyone shed any light on my problem?  
> > > > > Thanks,  
> > > > > R.

---

<div class="post-metadata">

**Author:** ![Rob\_Lally](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Rob\_Lally](https://rubytalk.org/u/Rob_Lally)\
**Post date:** [26 March 2005 19:37 UTC](https://rubytalk.org/t/model-object-in-session-problems/17183/5 "2005-03-26T19:37:30Z")

</div>

Ooops,

Actually that doesn't solve the problem. In my attempts to figure out what was wrong I had taken out the code that stored the object in the session and replaced it with a number. I can freely create User objects anywhere in the code so 'user' has already been required.

R.

Moses Hohman wrote:

> **···**
>
> > I assume that your login controller and your homepage controller are different classes? In that case, while your login controller may automatically require your user class definition, your homepage controller won't. If that doesn't make any sense, then I'm misunderstanding your situation and I'd have to see your code.
> > 
> > M
> > 
> > On Mar 26, 2005, at 11:31 AM, Rob Lally wrote:
> > 
> > > Thanks - that solves the problem.
> > > 
> > > I don't understand why it is necessary though. I assumed that the application had already required the User class and the user file since this is reading in something it just created and wrote out. What am I missing?
> > > 
> > > R.
> > > 
> > > Moses Hohman wrote:
> > > 
> > > > If you "require 'user'" in your application.rb (so that it will be required in every controller), the problem should go away. The problem is, as I understand it, that the homepage controller does not automatically know to require 'user'.  
> > > > On Mar 26, 2005, at 10:52 AM, Rob Lally wrote:
> > > > 
> > > > > Sorry - just realised I sent this to the ruby list rather than the rails list.
> > > > > 
> > > > > R.
> > > > > 
> > > > > Rob Lally wrote:
> > > > > 
> > > > > > Hi,  
> > > > > > I'm having a problem storing an AR model object in a session. Actually the problem doesn't seem to be related to storing it in a session. The problem occurs when the session is deserialised on the next request.  
> > > > > > When the user logs in I pop a User object (an AR model) into the session and redirect to the homepage. Then I see the following error  
> > > > > > \<ActionController::SessionRestoreError: Session contained objects where the class definition wasn't available. Remember to require classes for all objects kept in the session. The session has been deleted. (Original exception: undefined class/module User [ArgumentError])\>  
> > > > > > Can anyone shed any light on my problem?  
> > > > > > Thanks,  
> > > > > > R.
