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

**URL:** <https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627>\
**Category:** Rails\
**Created:** [March 12, 2008, 12:50am UTC](https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627 "2008-03-12T00:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 12, 2008, 12:50am UTC](https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627/1 "2008-03-12T00:50:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 12, 2008, 12:52am UTC](https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627/2 "2008-03-12T00:52:41Z")

</div>

> 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”.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 20, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627/3 "2008-03-20T14:11:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 21, 2008, 1:57am UTC](https://www.ruby-forum.com/t/where-to-set-a-variable-so-its-loaded-only-once-and-available-for-all-controllers/131627/4 "2008-03-21T01:57:41Z")

</div>

> 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.
