Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Rails know when to delete a record from the `sessions` table?

How does Rails know when to delete a record from the sessions table?

For example, if a user visits the website, a session record is created, but when the user closes the browser window, the server doesn't get any notification, so how does Rails delete records from sessions table?

like image 211
Zabba Avatar asked Mar 31 '11 18:03

Zabba


2 Answers

There are simplier ways:

ActiveRecord::SessionStore::Session.delete_all(["updated_at < ?", 12.hours.ago])

You can call it after some Action in Controller and this piece of code kill all dead sessions.

Of course it is disputable technique, but the simplest.

Full story

like image 110
Dzmitry Avatar answered Nov 15 '22 08:11

Dzmitry


You have to provide your own implementation of session expiration, to my understanding.

This API page gives you a pretty decent answer to how that works. You create your database table with the extra attributes created_on and updated_at (and you get timestamps for free) and then you just create a job to expire them whenever you want by looking at the updated_at attribute.

This post has details of how to run a job.

like image 39
Christopher WJ Rueber Avatar answered Nov 15 '22 09:11

Christopher WJ Rueber