# Database.yml question

**URL:** <https://www.ruby-forum.com/t/database-yml-question/129714>\
**Category:** Rails\
**Created:** [February 20, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/database-yml-question/129714 "2008-02-20T23:24:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmetcalfe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lmetcalfe](https://www.ruby-forum.com/u/lmetcalfe)\
**Post date:** [February 20, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/database-yml-question/129714/1 "2008-02-20T23:24:02Z")

</div>

I’m working on my first RoR app and have run into an issue with  
database.yml. Originally I had two schemas defined in this file. Now I  
need to add a third schema and RoR is not recognizing that it’s been  
added.

I’ve stopped and restarted the server (WeBrick), cleared my browser  
cache, and verified that there are no tabs in database.yml.

How can I get RoR to see the third schema has been added? Any help  
would be appreciated!

Thanks,  
Linda

From my database.yml…It’s not recognizing development\_db2 is there.

development\_db1:  
adapter: mysql  
database: db1\_development  
username: root  
password:  
host: localhost

development:  
adapter: mysql  
database: db\_development  
username: root  
password:  
host: localhost

development\_db2:  
adapter: mysql  
database: db2\_development  
username: root  
password:  
host: localhost

---

<div class="post-metadata">

**Author:** ![lmetcalfe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lmetcalfe](https://www.ruby-forum.com/u/lmetcalfe)\
**Post date:** [February 21, 2008, 12:53am UTC](https://www.ruby-forum.com/t/database-yml-question/129714/2 "2008-02-21T00:53:06Z")

</div>

what do you mean, saying “schema”, why do you need three development  
databases and why use webrick, when mongrel exists?  
maybe a bit of reading on “rails environment” will help you more, than  
anything else…

On 21 ÆÅ×, 00:24, Linda M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lmetcalfe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lmetcalfe](https://www.ruby-forum.com/u/lmetcalfe)\
**Post date:** [February 21, 2008, 5:18am UTC](https://www.ruby-forum.com/t/database-yml-question/129714/3 "2008-02-21T05:18:03Z")

</div>

Snipped some stuff for brevity…

Linda M. wrote:

> How can I get RoR to see the third schema has been added? Any help  
> would be appreciated!
> 
> From my database.yml…It’s not recognizing development\_db2 is there.
> 
> development\_db1:  
> adapter: mysql  
> database: db1\_development  
> username: root  
> password:  
> host: localhost
> 
> development\_db2:  
> adapter: mysql  
> database: db2\_development  
> username: root  
> password:  
> host: localhost

(First although its’ not WRONG what you have here, switch the naming  
convention to ‘name’\_‘environment’. That just seems to be the norm.)

Ex:

db2\_development:  
adapter: mysql  
etc etc etc.

Make sure you have a model class extending ActiveRecord::Base and have  
this class establish a connection (Using the attributes defined in your  
.yml config file automatically). Like so…

class Db1Base \< ActiveRecord::Base

# Prevents Single Table Inheritance from being ‘assumed’

self.abstract\_class = true

# Ties all extending models to this definition section in the

database.yml  
establish\_connection “db1\_#{RAILS\_ENV}”  
end

class Db2Base \< ActiveRecord::Base

# Prevents Single Table Inheritance from being ‘assumed’

self.abstract\_class = true

# Ties all extending models to this definition section in the

database.yml  
establish\_connection “db2\_#{RAILS\_ENV}”  
end

Now have all classes needing this DB extend the new Db1/2Base classes

Class SomeClassUsingTheDb1Definition \< Db1Base

# This would connect to the DB1 database.

end

Class SomeClassUsingTheDb2Definition \< Db2Base

# This would connect to the DB2 database.

end

You could put the establish\_connection in each Model class directly, but  
using a base class to extend from keeps the created connections to a  
minimum in your connection pool.

Hope this helps.

P.S. Thanks to Obie F. (The Rails Way), Rails Docs and PragDave  
for the info. (I had to read those sources a couple of times myself  
before I ‘got it’ the first time.

---

<div class="post-metadata">

**Author:** ![lmetcalfe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@lmetcalfe](https://www.ruby-forum.com/u/lmetcalfe)\
**Post date:** [February 21, 2008, 1:04am UTC](https://www.ruby-forum.com/t/database-yml-question/129714/4 "2008-02-21T01:04:11Z")

</div>

Ruby and rails are enjoyable, that’s why this programming has been  
rockin’

Linda, has you read the following links to answer your problem?

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

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

> **[Elctech.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=elctech.com)**
>
> Get a new domain name for your startup. Quick and professional service. Seamless domain transfers.

[http://www.railsonwave.com/railsonwave/2006/12/11/multiple-database-handlng-with-rails](http://www.railsonwave.com/railsonwave/2006/12/11/multiple-database-handlng-with-rails)

I prefer to read some articles in search engine (ex:google) before  
playing in forum.

Good Luck,

~_~_~_~_~_~_~_~_~_~_~_~_~  
Reinhart Ariando  
YM : Booking2Heaven  
Web: [teapoci.blogspot.com](http://teapoci.blogspot.com)  
~_~_~_~_~_~_~_~_~_~_~_~_~
