Topic: Advanced Session Stuff

So, I've been thinking about trying to change the sessions to make them more useful - I want add more fields into the database sessions table.  Basically:

user_id
controller, action, id <-- for tracking the page the user is currently on
ip <-- IP of the user, possibly to lock the session to that IP

possibly more, but I can't think of them at the moment.

So - does anyone have any ideas about how to do this?  I looked at the session code - It's in ruby itself, and not too complex.  What would be the easiest/best way to implement this.  I'm up for rewriting some ruby code, but I don't think it would be very easy - is the request and params object available there?

Thoughts?

-Drew

Last edited by drewt (2007-04-12 23:31:52)

Re: Advanced Session Stuff

This wouldn't need to be added to the sessions table.  The table contains a catch-all column for all of the session data.  If you want to store anything special in the session it's customary to just put it in your session object (session[:something] = 'something').  There's only enough fields in the session table to be able to connect the session data to a specific user - nothing else.

Re: Advanced Session Stuff

I know it can simply be stored in the session hash, but I wanted to store it in the table itself, then it'd make it easier to access from a model (eg: making a "who's online" screen).

I suppose I could just store the objects in the hash, then make a model that would retrieve them automatically.  Then I could provide delete functionality there with it - no problems just deleting a session row out of the database right?

Thanks
-Drew

Re: Advanced Session Stuff

There is a section in Agile Web Development with Rails, p97, which discusses and shows you how to store the session data in a database.  In fact if your application may run on more than one Rails server this is how they recommend you set it up.  Briefly you need to run a migration to create a sessions table, no fields required.  Then in config directory, in environment.rb, remove the comment on the line: config.action_controller.session_store = :active_record_store.  Then stop and restart your server.  Hope this helps...

Re: Advanced Session Stuff

Thanks, but I've already got active record store setup, I was trying to add IP and page info into the table.

Re: Advanced Session Stuff

drewt wrote:

I know it can simply be stored in the session hash, but I wanted to store it in the table itself, then it'd make it easier to access from a model (eg: making a "who's online" screen).

How much easier from session[:online] can it be ?