Friday, March 21, 2008

Rails: Session Cleaning code for removing sessions older than a given number of hours

I needed to figure out how to wipe the session table clean of all sessions older than a given number of hours.

num = num_hours * 3600

sql = ActiveRecord::Base.connection();
sql.begin_db_transaction
sql.execute("DELETE FROM sessions WHERE now() - updated_at > #{num}");
sql.commit_db_transaction

No comments: