[Rails] Accessing cookie and session objects

Norman Timmler
Fri, 03 Mar 2006 16:14:44 -0000

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