# Additional config file like database.yml

**URL:** <https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335>\
**Category:** Rails\
**Created:** [January 9, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335 "2009-01-09T15:58:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/1 "2009-01-09T15:58:31Z")

</div>

I have a web site that accesses Solr (a Lucene based search engine  
accessed via REST). I would like the configuration to be handled in  
the same was as database.yml. I envision a file called solr.yml that  
looks something like this in the config directory:

development:  
url: [http://dev.solr.example.com/search](http://dev.solr.example.com/search)

test:  
url: [http://test.solr.example.com/search](http://test.solr.example.com/search)

production:  
url: [http://production.solr.example.com/search](http://production.solr.example.com/search)

If I was to write this file how would I access it?

Any pointers would be appreciated as it would aid testing and I have a  
couple of other projects that could use this technique.

Thanks

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/2 "2009-01-09T17:35:19Z")

</div>

you could code it yourself, i think there is a railscast that shows an  
easy way to do it. or you could use one of the many plugins or gems  
out there. i just recently used

> **[GitHub - binarylogic/settingslogic: A simple and straightforward settings...](https://github.com/binarylogic/settingslogic/tree/master)**
>
> master

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/3 "2009-01-09T17:44:48Z")

</div>

You can do

solr\_conf = open(‘path/to/solr.yml’, ‘r’) { |f| YAML.load(f) }

solr\_conf now holds a hash with your parameteres. You can access it  
simply by doing something like:

url = solr\_conf[RAILS\_ENV][:url]

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/4 "2009-01-09T17:51:52Z")

</div>

Peter

On 9-Jan-09, at 11:44 AM, Harold wrote:

> On Jan 9, 11:33 am, scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > looks something like this in the config directory:  
> > > If I was to write this file how would I access it?
> > 
> > > Any pointers would be appreciated as it would aid testing and I  
> > > have a  
> > > couple of other projects that could use this technique.

If you take this approach, you can also embed erb in your yaml, and  
get a nice little object with your config at hand:

require ‘ostruct’  
raw\_config = File.read(“#{RAILS\_ROOT}/config/config.yml”)  
erb\_config = ERB.new(raw\_config).result  
config = YAML.load(erb\_config)[RAILS\_ENV]  
HomestarsConfig = OpenStruct.new(config)

so if you had a config setting of mail\_server,

you could access it HomestarsConfig.mail\_server

J

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/5 "2009-01-09T18:05:39Z")

</div>

Scott Nj wrote:

> you could code it yourself, i think there is a railscast that shows an  
> easy way to do it. or you could use one of the many plugins or gems  
> out there. i just recently used  
> [GitHub - binarylogic/settingslogic: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.](http://github.com/binarylogic/settingslogic/tree/master)

Yes, I believe there is a railscast on showing just what you’re looking  
for:

> **[\#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.

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [January 9, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/additional-config-file-like-database-yml/157335/6 "2009-01-09T18:17:44Z")

</div>

Thanks to everyone who replied. The railscast is just what I wanted.

Thanks to you all again.
