# Can't store object in session

**URL:** <https://www.ruby-forum.com/t/cant-store-object-in-session/134155>\
**Category:** Rails\
**Created:** [April 8, 2008, 11:57am UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155 "2008-04-08T11:57:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 8, 2008, 11:57am UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/1 "2008-04-08T11:57:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 8, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/2 "2008-04-08T13:00:07Z")

</div>

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/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 8, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/3 "2008-04-08T14:12:18Z")

</div>

Hey Julian,

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

This only fech the id, but does not work 😕  
@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.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 8, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/4 "2008-04-08T14:28:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 8, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/5 "2008-04-08T15:36:46Z")

</div>

Okay. Thanks. I got that working now 🙂

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 😕

Best regards.  
Asbjørn Morell.

On Apr 8, 2:18 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 9, 2008, 3:20am UTC](https://www.ruby-forum.com/t/cant-store-object-in-session/134155/6 "2008-04-09T03:20:53Z")

</div>

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/](http://sensei.zenunit.com/)
