Forum: Ruby on Rails newbie session confusions

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.
Russ McBride (Guest)
on 2006-01-08 12:20
(Received via mailing list)
I'm totally baffled by the relation between objects and the session
and when an object is still available.  It seems to me that there
should be a simple session.rb file (like the session.java file in
WebObjects).

Right now, I'm just trying to figure out how to keep an object
around, say a User object, without saving it to the db, so that I can
tell at any point whether the user is logged in.  I guess the answer
is:

     session[:user] = @user

where @user contains an @logged_in property.  But I have no idea when
I have to call:

      @user = session[:user]

  When is @user lost such that I need to grab it from the session hash
again?  When I change controllers?  Methods?  It seems that @user is
an instance variable of the controller class where it's declared so
@user shouldn't be lost between method calls within a single
controller but it is.  @user seems to be acting like a local
variable, user.

It doesn't seem like I can access the session hash from a model for
some reason.  Why not?

Also, If I generate a session_controller.rb does that have any kind
of special status or is it just like any other controller I generate?

Is there any reason to run the script/generate controller command
other than to save me a minute creating files (controller, helper,
view, test, etc.)?

thanks,
russ
Andreas S. (Guest)
on 2006-01-08 12:33
Russ McBride wrote:
>       @user = session[:user]
>
>   When is @user lost such that I need to grab it from the session hash
> again?

When a new request is started, e.g. in a before_filter. Each request
creates a new controller instance, you can't use instance variables
across requests.

> It doesn't seem like I can access the session hash from a model for
> some reason.  Why not?

Because that violates the Model-View-Controller concept.

> Also, If I generate a session_controller.rb does that have any kind
> of special status or is it just like any other controller I generate?

This shouldn't have any special status.

> Is there any reason to run the script/generate controller command
> other than to save me a minute creating files (controller, helper,
> view, test, etc.)?

No.
Vivek K. (Guest)
on 2006-01-09 06:08
(Received via mailing list)
On 1/8/06, Andreas S. <removed_email_address@domain.invalid> wrote:
>
> Russ McBride wrote:
> >       @user = session[:user]
> >


Also I think its a better idea to store just the user's unique id
instead of
the whole user object.
session[:user]=@user.id
I think( i am not too sure though) that session[:user]=@user will result
in
the entire instnace to be serialized using to_s and then stored on the
client's browser which can result in lots of memory usage on the server
because of the session cache if many users are logged on.
This topic is locked and can not be replied to.