# Getting database config info

**URL:** <https://www.ruby-forum.com/t/getting-database-config-info/99734>\
**Category:** Rails\
**Created:** [February 1, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/getting-database-config-info/99734 "2006-02-01T15:56:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robbie\_Carlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Robbie\_Carlton](https://www.ruby-forum.com/u/Robbie_Carlton)\
**Post date:** [February 1, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/getting-database-config-info/99734/1 "2006-02-01T15:56:44Z")

</div>

Hi folks. I have a very small problem which is probably very simple. But  
I’ve scoured the api and the rails book with no luck.  
I’m trying to get information about the database definition out of  
active  
record. When I go into console and say

ActiveRecord::Base.connection

I get a MysqlAdapter object, with an instance variable called @config  
that’s  
a hash containing all the information I need. However I can’t get at the  
config variable. I can think of two solutions, neither of which is  
entirely  
satisfactory. Either extend the definition of MysqlAdapter to include an  
accessor for database name, but I’m loathe to do this as it’s hacky and  
will  
cause problems with switching to different database types and updates to  
rails. The other is to parse the config file myself, but again this is  
clearly not clever as it’s duplication.

Thanks in advance

Robbie

---

<div class="post-metadata">

**Author:** ![Robbie\_Carlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Robbie\_Carlton](https://www.ruby-forum.com/u/Robbie_Carlton)\
**Post date:** [February 1, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/getting-database-config-info/99734/2 "2006-02-01T16:11:27Z")

</div>

I’ve asked a similar question, Peter has helped me as follow.

Hope this help you too

* * *

From: Peter D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) Mailed-By:  
[lists.rubyonrails.org](http://lists.rubyonrails.org)  
Date: Dec 22, 2005 10:50 PM  
Subject: Re: [Rails] [NEWB] How to access database.yml parameters in a  
controller

On 12/23/05, Mathieu C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are the active database.yaml attributes for the current environment  
> (dev, prod, test) available inside a controler ?

ActiveRecord::Base.configurations[RAILS\_ENV.to\_sym][‘database’]  
ActiveRecord::Base.configurations[RAILS\_ENV.to\_sym][‘host’]  
ActiveRecord::Base.configurations[RAILS\_ENV.to\_sym][‘adapter’]

---

<div class="post-metadata">

**Author:** ![Robbie\_Carlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Robbie\_Carlton](https://www.ruby-forum.com/u/Robbie_Carlton)\
**Post date:** [February 1, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/getting-database-config-info/99734/3 "2006-02-01T16:23:30Z")

</div>

thank you very much. I couldn’t get this to work in console

ActiveRecord::Base.configurations[RAILS\_ENV.to\_sym][‘database’]

I didn’t try it in a controller, but following your lead I did get this  
to  
work, which does exactly what I need

ActiveRecord::Base.configurations[‘development’][‘database’]

cheers

---

<div class="post-metadata">

**Author:** ![Robbie\_Carlton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e8c25b/32.png) [@Robbie\_Carlton](https://www.ruby-forum.com/u/Robbie_Carlton)\
**Post date:** [February 2, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/getting-database-config-info/99734/4 "2006-02-02T20:47:50Z")

</div>

I had some trouble finding these answers, because I couldn’t find out  
a decent set of keywords to search for. After finding the answer in  
this thread, I added it to [1]. If you will, please take a look and  
modify it in any way you see fit.

Thanks for providing the answer to my question,

- Rowan

[1] [http://wiki.rubyonrails.org/rails/pages/database.yml](http://wiki.rubyonrails.org/rails/pages/database.yml)
