Can't store object in session

Hello,

I got a session problem that I can’t solve.

session[:test] = “test session” works fine and can be printed from the
view.

session[:test] = User.find(1) returns true but the session is empty in
the view!
@data={:test=>#}, …

Session config:
config.action_controller.session = {
:session_key => ‘_www_session’,
:secret => ‘key removed…’
}

config.action_controller.session_store = :active_record_store

I found a thread on the RoR wiki that I should put model :user in
application.rb but that gives me an error. (depricated?)

Best regards.
Asbjørn Morell.

You’re better off storing the id in the session, and reloading from
that. I don’t think that session supports saving entire objects
without serializing / deserializing them. I’ve actually never tried to
do this, because it’s a bad practic e (IMHO).

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

Hey Julian,

Could you show me how I only store the id? That’s actually what I am
trying to do :slight_smile:

This only fech the id, but does not work :confused:
@user_id = find(:first, :select => [“id”], :conditions => [“login = ?
and password = ?”, login, Digest::SHA1.hexdigest(password)])

Checked the log and the query is correct and only return ex. 1

Best regards.
Asbjørn Morell.

On 8 Apr 2008, at 10:56, atmorell wrote:

@data={:test=>#}, …
The default to_s for an active record object looks like #<User: … >
which most browsers render as just #. If in your view you put <%= h
session[:test] %> you’d probably see what you were expecting
As Julian said putting whole model objects in the session isn’t a
great idea. If @user was your user you’d just stick @user.id in the
session.

Fred

Okay. Thanks. I got that working now :slight_smile:

How do I access an object from the view loaded by an application.rb
before_filter?

class ApplicationController < ActionController::Base

before_filter :user

def user
if session[:user]
@user = User.find(session[:user])
end
end

end

@user is working fine from my controllers but not in my views :confused:

Best regards.
Asbjørn Morell.

On Apr 8, 2:18 pm, Frederick C. [email protected]

Yeah, that won’t work.

You have no model on your find command. That makes no sense to rails
(active record, here).

Say I have a users table, and a User model.

@user_id = User.find(:first).id

or

user = User.find(:first)
@user_id = user.id

Either of those will find you the id.

ActiveRecord::Base.find will find a row, and create an object out of
it, so you need to ask the object what it’s ID is by sending it the id
message, as above.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/