Forum: Ruby on Rails Accessing cookie and session objects

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-03-03 15:14
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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-03 17:16
(Received via mailing list)
Am Freitag, den 03.03.2006, 15:14 +0100 schrieb Lindsay Boyd:
> 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 Timmler

http://blog.inlet-media.de
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-03-03 17:24
> 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
This topic is locked and can not be replied to.