Link to home
Start Free TrialLog in
Avatar of Syberye
SyberyeFlag for Romania

asked on

Session clean at RAILS server restart

I have some user informations stored in the session.
If i use Ctrl-c to kill the RAILS server and restart the server, the informations remains in the session which is stored in database.
I want the session informations gone, once i restart the server
Any suggestions?
How do I know when server has finised loading the enviroment. Will an event be trigger? if yes how can I catch this event?
Avatar of johanntagle
johanntagle
Flag of Philippines image

I guess you can create an initialized that will delete all records from the sessions table upon start.
Avatar of Syberye

ASKER

I can't delete the records from session table (because of bussiness rules) I just want to delete one element from the session. I have created a varible in application.rb and I check for that variable to see if the server has been restarted. Could You give me a better (optimaze) solution.

I want to modify the session without deleting it. Is there in RAILS an event that is triggered when the server finished restrating? I want to catch the moment when the server finised starting.

Tanks,
Avatar of Syberye

ASKER

I read the arcticle. Its nice , but unfortunatly it will not resolve my problem.
I will describe my problem more detailed.
The session is stored in the DB. After I close the server with ctrl+c and restart it , the session is restored from the DB.
My issue is, that i keep some data in the session that i want to keep, but some of the data needs to be deleted from it.
Ex: i want to keep the user related stuffs, but i want to delete the some data that is not important anymore.
Deleting all the records from the session table is not an option.
If i place a file in the "config/initializers/" as described, it will be loaded, but in that moment i dont have the session.
So if i place a "puts session.inspect" or "puts session[:user_id]" i get an "undefined local variable or method `session' for main:Object" error.
So that means that the session was not retrieved yet.
The main problem is that in the application_controller i have a call that will return the session data , like this: session[:something].
That must be nil after the server has been restarted.
What i want to do is the following:
- catch an event that is triggered when the server has been restarted , but after the session is restored so i can delete my ":something"
- or write a methode that will be called only once in the application after the server has been restarted.

For now i have implemented a quick fixed (its a dirty one): have added in the application.rb config.server_start = true.
Every time that i will call the function from application_controller i will check if this flag is true and set it to false and delete the ":something".
I need a clean version , a nice implementation

Thanks,
I would created an step in perhaps either your application controller or 'root' controller like the below:

def clean_data
  session[:userdata] = nil
end

Basically you just need to nil the session data you want to remove.
I do this in my login controller, to nil the user_id, and other data stored throughout the system, in case one user logs out and another logs in from the same browser session, the previous user's data is no longer available.

Hope this helps,
Andrew
Avatar of Syberye

ASKER

Hi,

Thanks for the info .But unfortunatly this will not resolve my problem because at server restart the user will not be asked to login again, because the session is restored (with the user_id in the session, so automaticly the application will see as a logged in user).
I searched for a solution to catch the moment the server has loaded the session from the DB. Is there something ? An event ? A file that is loaded at that moment ? Or an event that is triggered when the server has finished loading the environment (and where i have a session) ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Andrew Doades
Andrew Doades
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Syberye

ASKER

It's not realy okay

I want to get only the session which was create by that instance of server - which was restart. I don't want to tuch other session from other instance of servers. Basic I want to modify some elements from session when the one server was restarted (only the session which was create by that instance of server). Where can I put some code and how can I get only the sessions created by my instant of restarted server ?
Avatar of Syberye

ASKER

I't not do what realy want.