Forum: Ruby on Rails Access session in models

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5d1e9b2f35a18d362a714413c194303f?d=identicon&s=25 Kwi (Guest)
on 2007-06-04 12:04
Hi guys,

I have a little problem, i want to access the session in a model.
I have a variable of that model in session and i want to keep my session
update after each modification, and i want to centralize that in the
Do you know how i can do that ?

8608f2d7f02c6d0f1c349b7b94b1cea5?d=identicon&s=25 liquidautumn (Guest)
on 2007-06-04 14:28
(Received via mailing list)
Your problem is a bit bigger than you think. In fact, you shouldn't do
that. Model knows nothing about controller and it is how it supposed
to be.
Just an advice - when you are going to ask question until you learning
something, don't try to provide abstract explanation, be as specific
as you can.
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-06-04 18:25
(Received via mailing list)
You can use after filters on your actions or similar methods to ensure
that updates to the model occur after each action.  The controller
should call the model method to make any changes.  This way the model
can be used with multiple controllers and they are not coupled.  Rails
makes this type of best practice a hard boundary by not allowing
models access to controller data except by the controller invoking
model methods.  The only other thing you could look into is
observers.  They have access to both the controller and model, but are
only invoked when a change occurs to a model they are registered for.

67b18e0e6de269790dee456a33ea1d31?d=identicon&s=25 Kevin Skoglund (Guest)
on 2007-06-04 20:01
(Received via mailing list)
Both of the above are correct.

An even easier answer to your question is that you should access the
session in your controller, then pass that information into the model
via a method.

Kevin Skoglund
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-06-05 02:44
(Received via mailing list)
Right, and furthermore, the decoupling of Controller and Model really
should be absolute in any MVC system.  Think of it this way.  If you
yanked the model objects out of the application and dropped them in
another application (hypothetically), would they still work? If they
were coupled with the session object they would not (just to be clear
this hypotheical receiving application has no Session class).  Your
MVC breaks down at a fundamental level.

On the other hand it is generally more complicated to decouple
Controller and View (yet not impossible).  Most MVC based frameworks
don't tent to attempt this decoupling.  That being said, it is still
good practice to make every effort to decouple even these.

Generally speaking, the Controller is the least reusable component of
the MVC, since they tend to contain most of the code that is specifc
to a particular application.  Model objects on the other hand should
be fully reuseable, requiring clear decoupling from the rest of the
application code.
This topic is locked and can not be replied to.