# Accessing cookie and session objects

**URL:** <https://www.ruby-forum.com/t/accessing-cookie-and-session-objects/76240>\
**Category:** Rails\
**Created:** [March 3, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/accessing-cookie-and-session-objects/76240 "2006-03-03T15:14:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 3, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/accessing-cookie-and-session-objects/76240/1 "2006-03-03T15:14:34Z")

</div>

Is it possible to directly access session and cookie objects from within  
a Model? When I try to read the session object in my User Model I get  
the following error:

NameError (undefined local variable or method `session’ for User:Class):

I could pass attributes taken from the session into my method in the  
User Model, but this seems to go against DRY.

-Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 3, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/accessing-cookie-and-session-objects/76240/2 "2006-03-03T17:16:04Z")

</div>

Am Freitag, den 03.03.2006, 15:14 +0100 schrieb Lindsay B.:

> Is it possible to directly access session and cookie objects from within  
> a Model? When I try to read the session object in my User Model I get  
> the following error:
> 
> NameError (undefined local variable or method `session’ for User:Class):
> 
> I could pass attributes taken from the session into my method in the  
> User Model, but this seems to go against DRY.

No, ActiveRecord models have no direct access to the ActionController  
objects or methods. Handling sessions or cookies within the model would  
breach against the MVC (Model, View, Controller) principle what is the  
base Rails is build upon. It goes over DRY.

If you need some information about session parameters in the model, pass  
them to model methods.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 3, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/accessing-cookie-and-session-objects/76240/3 "2006-03-03T17:24:38Z")

</div>

> Handling sessions or cookies within the model would  
> breach against the MVC (Model, View, Controller) principle what is the  
> base Rails is build upon. It goes over DRY.

Yes, I suppose MVC should trump DRY. I’ll just send my session  
attributes to the Model.

thanks  
-Lindsay
