# Rails 2.3 Sessions

**URL:** <https://www.ruby-forum.com/t/rails-2-3-sessions/163946>\
**Category:** Rails\
**Created:** [March 26, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/rails-2-3-sessions/163946 "2009-03-26T12:30:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan\_sp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@dan\_sp](https://www.ruby-forum.com/u/dan_sp)\
**Post date:** [March 26, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/rails-2-3-sessions/163946/1 "2009-03-26T12:30:34Z")

</div>

I’ve just upgraded to Rails 2.3.2. I am using active\_record\_store as  
my session approach and session.model is deprecated. I was using that  
to update a couple extra session db columns. Does anyone knows what is  
the best approach for that now, since ActiveRecord::SessionStore does  
not help that much?

thanks!

---

<div class="post-metadata">

**Author:** ![dan\_sp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@dan\_sp](https://www.ruby-forum.com/u/dan_sp)\
**Post date:** [April 2, 2009, 10:58am UTC](https://www.ruby-forum.com/t/rails-2-3-sessions/163946/2 "2009-04-02T10:58:30Z")

</div>

On Mar 26, 12:45 pm, Danilo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve just upgraded to Rails 2.3.2. I am using active\_record\_store as  
> my session approach and session.model is deprecated. I was using that  
> to update a couple extra session db columns. Does anyone knows what is  
> the best approach for that now, since ActiveRecord::SessionStore does  
> not help that much?

I had the exact same problem, and I couldn’t find any help anywhere.  
So I played with it until I got it to work. This is what I did to put  
the user id in the sessions table upon login:

In session.rb:

class Session \< ActiveRecord::Base  
def self.update\_user\_id(id, sess\_id)  
connection.update(“UPDATE sessions SET user\_id = ‘#{id}’ WHERE  
session\_id = ‘#{sess\_id}’”)  
end  
end

In login action in the controller:

if session[:userid] = User.authenticate(params[:user][:username],  
params[:user][:password])  
Session.update\_user\_id(session[:userid], request.session.session\_id)  
[…]

IOW, this:

session.model.user\_id = session[:userid]

becomes this:

Session.update\_user\_id(session[:userid], request.session.session\_id)

Apparently one has to call “session[:userid]” or in order to  
“activate” the session since it is now lazy (ie it won’t load unless  
you access it). Otherwise, the “request.session.session\_id” part won’t  
work.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![dan\_sp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c4cdca/32.png) [@dan\_sp](https://www.ruby-forum.com/u/dan_sp)\
**Post date:** [April 3, 2009, 10:14pm UTC](https://www.ruby-forum.com/t/rails-2-3-sessions/163946/3 "2009-04-03T22:14:55Z")

</div>

Your suggestion helped a lot! I’ve followed a slightly different  
approach,  
but using your idea. What I did was add the following private method in  
my  
login controller:

def session\_user\_id(id)  
ActiveRecord::Base.connection.update(“UPDATE sessions SET user\_id =  
#{id} WHERE session\_id = ‘#{request.session.session\_id}’”)  
end

And then I just do this in the login action:

session[:user] = user.id  
session\_user\_id(session[:user])

I think that there is an advantage in this approach. No need to deal  
with  
any custom session classes and the method is only available where it  
should  
be.

cheers!
