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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With