# Store common settings in one file?

**URL:** <https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217>\
**Category:** Rails\
**Created:** [August 3, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217 "2006-08-03T16:53:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 3, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/1 "2006-08-03T16:53:58Z")

</div>

Hi,

I’m looking for a way to store common settings, such as the  
application\_name or email address from value in a file.

I then want to be able to call these settings in my controllers and  
models?

Is there any easy way of doing this? and where would you recommend I  
save the file ?

Thanks  
scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 3, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/2 "2006-08-03T17:43:24Z")

</div>

Put the file in the lib directory and then just require it where you  
need it. You could also make it a Module which may be loaded for you  
and then you can just include it.

lib/application-info.rb

module ApplicationInfo  
attr\_reader :application\_name, :application\_email  
@application\_name = “My Cool App”  
@application\_email = “removed\_email\_address@domain.invalid”  
end

class SomethingController \< AC::Base  
include ApplicationInfo

```
# now your actions and views have access to
# @application_name and @application_email

```

end

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 3, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/3 "2006-08-03T18:22:35Z")

</div>

Carl F. wrote:

> Put the file in the lib directory and then just require it where you  
> need it. You could also make it a Module which may be loaded for you  
> and then you can just include it.
> 
> lib/application-info.rb
> 
> module ApplicationInfo  
> attr\_reader :application\_name, :application\_email  
> @application\_name = “My Cool App”  
> @application\_email = “removed\_email\_address@domain.invalid”  
> end
> 
> class SomethingController \< AC::Base  
> include ApplicationInfo
> 
> ```
> # now your actions and views have access to
> # @application_name and @application_email
> 
> ```
> 
> end

thanks carl,

Will give it a go

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 4, 2006, 12:42am UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/4 "2006-08-04T00:42:25Z")

</div>

Hi Scott,

I would go for something much simpler, in environnment.rb:  
CONFIG = {  
:email =\> ‘removed\_email\_address@domain.invalid’,  
}

Then, calling from everywhere in the app: CONFIG[:email]

Jean-Etienne

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 4, 2006, 4:07am UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/5 "2006-08-04T04:07:04Z")

</div>

I just stick my common settings into environment.rb, such as:

SUPPORT\_EMAIL = “removed\_email\_address@domain.invalid”

then I can use “mail\_to(SUPPORT\_EMAIL)” inside my app

Mike

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [August 4, 2006, 2:56am UTC](https://www.ruby-forum.com/t/store-common-settings-in-one-file/101217/6 "2006-08-04T02:56:39Z")

</div>

There are numerous plugins for this. Naively I wrote this code that I  
put in  
/lib. You can get to it from anywhere and it provides a straightforward  
array-like interface. E.g.,

Options[:email\_from] = ‘removed\_email\_address@domain.invalid’  
Options[:smtp\_server] = ‘[smtp.mydomain.com](http://smtp.mydomain.com)’

HTH

# Provide a container for global option storage

# Both getter/setter and [] access methods are

# provided.

class Options  
@@option\_hash = {}

def self.set(option\_name, value)  
option\_name = option\_name.to\_s  
@@option\_hash[option\_name] = value  
end

def self.get(option\_name)  
option\_name = option\_name.to\_s  
@@option\_hash[option\_name]  
end

def self.[]=(option\_name, value)  
option\_name = option\_name.to\_s  
self.set(option\_name, value)  
end

def self.  
option\_name = option\_name.to\_s  
self.get(option\_name)  
end

def self.has\_key?(key)  
key = key.to\_s  
@@option\_hash.has\_key?(key)  
end

## def self.key?(key) self.has\_key?(key) end end

View this message in context:  
[http://www.nabble.com/Store-common-settings-in-one-file--tf2046127.html#a5643220](http://www.nabble.com/Store-common-settings-in-one-file--tf2046127.html#a5643220)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
