# Access to database.yaml elements

**URL:** <https://www.ruby-forum.com/t/access-to-database-yaml-elements/90332>\
**Category:** Rails\
**Created:** [March 27, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/access-to-database-yaml-elements/90332 "2007-03-27T22:11:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [March 27, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/access-to-database-yaml-elements/90332/1 "2007-03-27T22:11:56Z")

</div>

I’d like to be able to display which db is the active db as  
I have a couple of development databases that I switch between,  
and I’d like the interface to remind me which one I’m dealing with by  
displaying some of.

Can someone point me somewhere where I can have it show me the name of  
the database that’s being accessed?

Thanks,

-Al

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [March 27, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/access-to-database-yaml-elements/90332/2 "2007-03-27T22:33:24Z")

</div>

You could do it like this:

require ‘yaml’  
config = YAML.load(File.open(‘config/database.yml’))  
config[‘development’][‘database’]

Cheers,

Mischa

–  
[http://boxroom.rubyforge.org](http://boxroom.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [March 27, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/access-to-database-yaml-elements/90332/3 "2007-03-27T22:34:03Z")

</div>

> I’d like to be able to display which db is the active db as  
> I have a couple of development databases that I switch between,  
> and I’d like the interface to remind me which one I’m dealing with by  
> displaying some of.
> 
> Can someone point me somewhere where I can have it show me the name of  
> the database that’s being accessed?

I can’t, but here’s the code we use to read our ‘site.yml’ file (for  
local  
config stuff) that follows the exact same format as database.yml…

SITE\_YAML = YAML::load(ERB.new(IO.read("#{RAILS\_ROOT}/config/site.yml"),  
nil, ‘-’).result)  
SITE\_YAML\_ENV = SITE\_YAML[RAILS\_ENV]

So assuming you switched that over to database.yml then you could spit  
out

SITE\_YAML\_ENV[‘database’]

and you’d have your current database…

-philip
