Where to set a variable so it's loaded only once and available for all controllers?

Hi,

I'm using GeoIP that loads database file with countries when the GeoIP object is created. Where should I initialize this object, so it would be loaded only once (not for every request) and available for all controllers?

The code looks like this: geo_ip = GeoIP.new("#{RAILS_ROOT}/public/GeoIP.dat")

Thanks in advance

I'm using GeoIP that loads database file with countries when the GeoIP object is created. Where should I initialize this object, so it would be loaded only once (not for every request) and available for all controllers?

The code looks like this: geo_ip = GeoIP.new("#{RAILS_ROOT}/public/GeoIP.dat")

Tail end of environment.rb. Or in one of the new initializer hooks that helps keep it out of environment.rb.

You may want to make that GEO_IP to make it more obvious it's "site wide".

If you put this into environment.rb or one of the new initializer hooks how to prevent this from running for example if you do a rake db:migrate? Chances are that your tables (for your ActiveRecord Objects) are not there and everything blow up...

Is there a way to ask rails if we run our environment as a migration?

Feurio

I'm using GeoIP that loads database file with countries when the GeoIP object is created. Where should I initialize this object, so it would be loaded only once (not for every request) and available for all controllers?

You could put in the Application Controller.