# How to Have Constants Available Throughout App?

**URL:** <https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242>\
**Category:** Rails\
**Created:** [June 23, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242 "2008-06-23T23:21:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 23, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242/1 "2008-06-23T23:21:10Z")

</div>

I discovered that you can add these modules under /lib with constants in  
them and call these constants like so :

module AdminConstants

email =‘removed\_email\_address@domain.invalid’  
phone =“123456789”

end

and then access them using AdminConstants::email

sometimes I can put in a require “adminConstants” in order to access  
them. I am thinknig of putting them all over the place. Whereever there  
is any value hardcoded. Can I use this in development.rb?

How about if I wanted different set of constants for different  
environments?

like x=1;y=2 in development but x=2,y=3 in production?

Can I have the right files load during startup? Is it possible to  
initialize the constants in the lib modules with values from the  
database?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 12:11am UTC](https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242/2 "2008-06-24T00:11:34Z")

</div>

Why not add these to a YAML config file? Check out this Railscast

> **[\#85 YAML Configuration File - RailsCasts](http://railscasts.com/episodes/85-yaml-configuration-file)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 12:12am UTC](https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242/3 "2008-06-24T00:12:45Z")

</div>

You could put the constants in config/environment.rb:

if RAILS\_ENV == “development”  
EMAIL = “removed\_email\_address@domain.invalid”  
PASSWORD = “1234567890”  
else  
EMAIL = “removed\_email\_address@domain.invalid”  
PASSWORD = “0987654321”  
end

And then just access them as EMAIL and PASSWORD in your application.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 1:51am UTC](https://www.ruby-forum.com/t/how-to-have-constants-available-throughout-app/141242/4 "2008-06-24T01:51:47Z")

</div>

Ather S. wrote:

> How about if I wanted different set of constants for different  
> environments?
> 
> like x=1;y=2 in development but x=2,y=3 in production?
> 
> Can I have the right files load during startup? Is it possible to  
> initialize the constants in the lib modules with values from the  
> database?

Here’s what I do:

I have a file config/app\_config.rb in which I have all of my settings in  
a hash. It looks like this (formatting might be messed up some):

=========================================

=begin  
NOTE: Changes to these settings will not take effect until the server  
is bounced.

Description of configuration settings  
admin\_email: where all of the administrative emails go  
load\_google\_analytics: disable/enable Google Analytics  
=end

# SHARED contains settings that are shared across both environments and

are unlikely to

# be different. A good example of this is the google\_urchin\_path. When

loading the urchin.js

# from google’s server, it would be the same for both environments

(whether it should be loaded

# in development is another matter). However, if a SHARED value needs to

be overridden, just add

# the :key =\> value pair to the appropriate environment.

SHARED = {  
:local\_urchin\_path =\> nil,  
:google\_urchin\_path =\> ‘[http://www.google-analytics.com/urchin.js](http://www.google-analytics.com/urchin.js)’,  
:use\_local\_urchin =\> false,  
:default\_time\_zone =\> ‘Central Time (US & Canada)’  
}

CONFIG = {  
:development =\> {  
:admin\_email =\> ‘removed\_email\_address@domain.invalid’,  
:load\_google\_analytics =\> false  
},  
:production =\> {  
:admin\_email =\> ‘removed\_email\_address@domain.invalid’,  
:load\_google\_analytics =\> true  
}  
}

# write the specific environment configuration to a single hash that can

be

# accessed easier in the application. With this, we can do

# APP\_CONFIG[:admin\_email]

# instead of

# CONFIG[RAILS\_ENV.to\_sym][:admin\_email]

APP\_CONFIG = SHARED.merge(CONFIG[RAILS\_ENV.to\_sym])

============================================

In config/environment.rb, I do

require ‘app\_config’

which means the constant hash APP\_CONFIG will be available anywhere I  
need it. So I can do stuff like

class AdminMailer \< ActionMailer::Base  
def contact\_us\_email(params)  
@subject = “Contact Us: #{params[:contact\_subject].strip}”  
@recipients = APP\_CONFIG[:admin\_email]  
@cc = params[:contact\_email].strip if params[:contact\_copy\_self]  
@from = “#{params[:contact\_name].strip}  
\<#{params[:contact\_email].strip}\>”  
@sent\_on = TimeZone.new(APP\_CONFIG[:default\_time\_zone]).now  
@headers = {}  
@body = params[:contact\_body].strip  
end  
end

This approach works extremely well for me.

Peace,  
Phillip
