Topic: Load session variable at startup

I have a bunch of actions that require session[:season] to be set. Since I can't guarantee a user will enter the site at a given point, I'm using this line for multiple actions:

@season = session[:season] || load_season

... where load_season uses the current session[:season] if it's not set or otherwise pulls it from the database.

This isn't awfully bad, but it's not very DRY, since I have to repeat this line for many actions. I also have to create @season instead of directly referencing session[:season].

In PHP, I would have just set the session var in an early line of code which would guarantee that it was set.

Is there any equivalent in Rails -- a location that says "do this code before doing anything else"?


Re: Load session variable at startup

Try doing that in a before_filter on the ApplicationController (/app/controllers/application.rb).

vinnie - rails forum admin

Re: Load session variable at startup

Excellent, thanks, vin. I had thought of that and then forgotten to try it.