# Initialize Static Data on Startup

**URL:** <https://www.ruby-forum.com/t/initialize-static-data-on-startup/117346>\
**Category:** Rails\
**Created:** [October 16, 2007, 4:58am UTC](https://www.ruby-forum.com/t/initialize-static-data-on-startup/117346 "2007-10-16T04:58:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![acerpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@acerpro](https://www.ruby-forum.com/u/acerpro)\
**Post date:** [October 16, 2007, 4:58am UTC](https://www.ruby-forum.com/t/initialize-static-data-on-startup/117346/1 "2007-10-16T04:58:20Z")

</div>

Hi,

I want to initialize some variables once on startup with static data  
from the db. The code is at the bottom. Here are all the scenarios I  
tested:

1. Call Setup.state\_list inside environment.rb before the  
Rails::Initializer.run do |config| line. Then call the  
Setup.state\_list from a controller.
2. Call Setup.state\_list inside environment.rb at the end of the file.  
Then call the Setup.state\_list from a controller.
3. Only call the Setup.state\_list from a controller.
4. Only call the Setup.state\_list from a view helper - never in the  
controller.

Please not that I’ve tried this with Setup being a class or module. The  
results from the scenarios are:

1 & 2) Every time I call the Setup.state\_list from the controller it  
will rebuild the list.  
3) It will only initialize the variable the first time through.  
4) Every time I call the Setup.state\_list from the view helper it will  
rebuild the list.

Great - so it appears that scenario 3 works. The problem is that this  
isn’t done at startup. I don’t want the system to start if it can’t  
initialize this and other information. Is something going on when I  
call it from the environment.rb file that I’m not aware of? I know the  
code could be written better, however, it does work. Does anyone have  
any thoughts on how to do this?

Thanks in advance!

==========================  
[i]  
class Setup

# What attributes do we have

@@state\_list = nil

#################################

# Public Section

#################################

def self.state\_list  
if @@state\_list.nil? || @@state\_list.size \<= 0  
RAILS\_DEFAULT\_LOGGER.debug(“init state now”)  
self.init\_state  
end

```
RAILS_DEFAULT_LOGGER.debug("sending back stateList -

```

#{@@state\_list.size}")  
@@state\_list  
end

#################################

# Private Section

#################################

private

def self.init\_state  
RAILS\_DEFAULT\_LOGGER.info(“Initializing state list”)  
states = State.find( :all, :order =\> “country\_id asc, label asc”)  
@@state\_list = []  
if states.nil? || states.size \<= 0  
RAILS\_DEFAULT\_LOGGER.error(“Unable to load states for  
initialization”)  
# TODO  
else  
states.each { |s|  
if @@state\_list[s.country\_id].nil?  
@@state\_list[s.country\_id] = [s]  
else  
@@state\_list[s.country\_id].concat( [s] )  
end  
}  
end  
end  
[/i]
