# Best way to set up application settings?

**URL:** <https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847>\
**Category:** Rails\
**Created:** [August 21, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847 "2006-08-21T21:25:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 21, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847/1 "2006-08-21T21:25:28Z")

</div>

I nver saw anything about this, but what about settings for your  
application? Where do you put them?

I don’t think it makes sense to store setting in the database because  
let’s say I made a settings table. I would only have one row in there,  
which would be the settings values. That seems like a hack to me.

Is it best to just store them in a yaml file? The problem I have with  
this is that I don’t get all of the advantages ActiveRecord gives me.  
Like changing integers to integers, booleans to booleans, datetime field  
to Time objects, etc. I also get a nice set of validation tools so I can  
make sure a proper value is inserted when editing from within the  
application.

Does anyone have a good solid settings recipe or at least some  
suggestions to get me going in the right direction without having to  
spend 10 hours writing a settings model.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 21, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847/2 "2006-08-21T22:34:27Z")

</div>

On Aug 21, 2006, at 12:25 PM, Ben J. wrote:

> Like changing integers to integers, booleans to booleans, datetime  
> Thanks for your help.  
> Hey Ben-

```
Here is a model I use for settings and stuff like you are talking

```

about. I wanted to be able to edit my configs in the interface so I  
store it in a model but it has an interface like a hash:

class DbHash \< ActiveRecord::Base  
class \<\< self  
def   
pair = find\_by\_key(key.to\_s)  
pair.value unless pair.nil?  
end

```
 def []=(key, value)
   pair = find_by_key(key)
   unless pair
     pair = new
     pair.key, pair.value = key.to_s, value
     pair.save
   else
     pair.value = value
     pair.save
   end
   value
 end

 def to_hash
   Hash[*find_all.map { |pair| [pair.key, pair.value] }.flatten ]
 end

```

end  
end

```
 create_table "configurations", :force => true do |t|
   t.column "key", :string, :limit => 40, :default => "", :null

```

=\> false  
t.column “value”, :string, :default =\> “”  
end

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 21, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847/3 "2006-08-21T22:42:56Z")

</div>

Ezra Z. wrote:

> On Aug 21, 2006, at 12:25 PM, Ben J. wrote:
> 
> > Like changing integers to integers, booleans to booleans, datetime  
> > Thanks for your help.  
> > Hey Ben-
> 
> Here is a model I use for settings and stuff like you are talking  
> about. I wanted to be able to edit my configs in the interface so I  
> store it in a model but it has an interface like a hash:
> 
> class DbHash \< ActiveRecord::Base  
> class \<\< self  
> def   
> pair = find\_by\_key(key.to\_s)  
> pair.value unless pair.nil?  
> end
> 
> ```
> def []=(key, value)
> pair = find_by_key(key)
> unless pair
> pair = new
> pair.key, pair.value = key.to_s, value
> pair.save
> else
> pair.value = value
> pair.save
> end
> value
> end
> 
> def to_hash
> Hash[*find_all.map { |pair| [pair.key, pair.value] }.flatten ]
> end
> 
> ```
> 
> end  
> end
> 
> ```
> create_table "configurations", :force => true do |t|
> t.column "key", :string, :limit => 40, :default => "", :null
> 
> ```
> 
> =\> false  
> t.column “value”, :string, :default =\> “”  
> end
> 
> Cheers-  
> -Ezra

Thanks a lot Ezra. Let’s say you store an integer in the DB will this  
pull it out and run the .to\_i method? so I can do some\_config \< 5 and  
not get a comparison of string error.

If not I’m sure that would be easy to implement. I could implment that  
and post the changes here.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 21, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847/4 "2006-08-21T23:12:31Z")

</div>

An alternative approach that we’ve tried in a couple of our apps is to  
extend Rails::Configuration to add our own settings. The nice thing  
about  
this approach is it lets us easily have different settings for different  
environments, with defaults and everything, just like the built in rails  
configuration options, and all checked in to svn. The downside is that  
we  
haven’t been able to figure out a way to do it without creating a global  
reference to the config object, which creates some awkward coupling  
between  
the config and whatever uses it, such as model objects. Its pretty easy  
to  
do, though.

in environment.rb:

class MyConfig \< Rails::Configuration  
#define attributes or whatever  
end

MY\_CONFIG = MyConfig.new  
Rails::Initializer(:process, MY\_CONFIG) do |config|  
#all the usual config stuff  
config.my\_custom\_setting = “default value”  
end

Then wherever you need the configuration value, you can check it:

def some\_method  
filename = MY\_CONFIG.my\_custom\_setting  
end

We also tried a more dependency injection based approach, where the  
config  
object would set values for the classes that needed them. We ran into  
problems when classes are reloaded in development mode, though -  
everything  
would work fine on the first request, but on subsequent requests the  
class  
would have been reloaded, losing the values that had been set before,  
but  
the environment code does not get re-run so we would end up not having  
the  
values set at all.

Just an idea, and Ezra’s approach will certainly work great for storing  
the  
values in the database.

-Steve H.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [August 22, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/best-way-to-set-up-application-settings/65847/5 "2006-08-22T14:34:00Z")

</div>

[http://wiki.rubyonrails.org/rails/pages/SettingsPlugin](http://wiki.rubyonrails.org/rails/pages/SettingsPlugin)

Very easy to use. Can be used to setup user specific settings, as  
well as applicatoin-wide settings. Since the settings are in the db  
you can change setting without restarting the app, as you would have  
to if they were in environment.rb.

–  
(\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- Peter H. Boling
- Web Application Designer - PanEther, LLC
- email: removed\_email\_address@domain.invalid
- blog: [http://galtzo.blogspot.com/](http://galtzo.blogspot.com/)
- languages: English, Spanish, Portuguese  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*)
