# Multiple databases + switching databases

**URL:** <https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106>\
**Category:** Rails\
**Created:** [March 17, 2006, 12:26am UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106 "2006-03-17T00:26:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 17, 2006, 12:26am UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/1 "2006-03-17T00:26:26Z")

</div>

Hello,

I have an application that access two different DBMS - Firebird  
and Postgres. So far, that’s ok. The problem is, depending on the  
customer logged, a different database (Firebird) must be selected.

To make the things clearer:

Postgres: 1 database shared by all customers with these tables:  
- user  
- vehicle  
- driver

Firebird: several databases with different names and identical structure  
with this table:  
- gps

Each customer has a firebird database. The name of the customer  
determines the database name:

```
- Customer name: C1 - Database name: C1.fdb
- Customer name: C2 - Database name: C2.fdb
- Customer name: C3 - Database name: C3.fdb
...

```

Model classes:

```
- User (postgres)
- Vehicle (postgres)
- Driver (postgres)

- Gps (firebird)

```

If the customer C1 is logged, the Gps class must be associated to  
table Gps in C1.fdb, C2.fdb if customer C2, and so on.

How could I do this choice at runtime?  
Is there a way to set table name in environment.rb file dynamically?  
Is there another way to switch the database without setting  
environment.rb?  
I’ve tried to do this just setting connection at Gps class but it didn’t  
work.

( ActiveRecord::Base.establish\_connection(  
:adapter =\> “firebird”,  
:host =\> “localhost”,  
:database =\> customer\_name,  
:username =\> “SYSDBA”,  
:password =\> “masterkey”)

Does anybody have any idea?

Thanks a lot!

Marcia

---

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 17, 2006, 5:16am UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/2 "2006-03-17T05:16:50Z")

</div>

> ( ActiveRecord::Base.establish\_connection(  
> :adapter =\> “firebird”,  
> :host =\> “localhost”,  
> :database =\> customer\_name,  
> :username =\> “SYSDBA”,  
> :password =\> “masterkey”)

Can you provide some more detail on how establish\_connection is  
failing?

> Does anybody have any idea?

I would think tat you’re on the right track with establish\_connection;  
if you haven’t yet, take a look at:

[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases)  
[http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc)

---

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 17, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/3 "2006-03-17T14:39:48Z")

</div>

Hello, Frank

In a page, I have two combos (one of them listing all vehicles and the  
other, all drivers); to populate these combos, I use Vehicle.find\_all  
and Driver.find\_all. Both model classes must use Postgres connection as  
well as all the other class, BUT Gps (that uses firebird). When I  
request that page, the system tries to connect to firebird, but that  
tables are in postgres. I was thinking the problem could be the  
following: I put the clause ActiveRecord::Base.establish\_connection (to  
firebird) just in Gps class. Maybe I  
have to put the postgres establishment of connection in the others  
classes, too. As far as I’ve read, I understood I had to do the  
‘mapping’ just to Gps class. But I really don’t know. I’ll try to do the  
test and tell you what I get. If you think I’m in the wrong way, please,  
tell me.

Thanks a lot, again

Marcia

Frank Cameron wrote:

> > ( ActiveRecord::Base.establish\_connection(  
> > :adapter =\> “firebird”,  
> > :host =\> “localhost”,  
> > :database =\> customer\_name,  
> > :username =\> “SYSDBA”,  
> > :password =\> “masterkey”)
> 
> Can you provide some more detail on how establish\_connection is  
> failing?
> 
> > Does anybody have any idea?
> 
> I would think tat you’re on the right track with establish\_connection;  
> if you haven’t yet, take a look at:
> 
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases)  
> [http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc)

---

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 17, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/4 "2006-03-17T23:19:01Z")

</div>

> ‘mapping’ just to Gps class. But I really don’t know. I’ll try to do the  
> test and tell you what I get. If you think I’m in the wrong way, please,  
> tell me.

I think it should be sufficient to have the default in database.yml  
point to the Postgres database and explicitly connect to Firebird  
from the Gps class.

---

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 17, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/5 "2006-03-17T23:46:21Z")

</div>

> > In a page, I have two combos (one of them listing all vehicles and the  
> > other, all drivers); to populate these combos, I use Vehicle.find\_all  
> > and Driver.find\_all. Both model classes must use Postgres connection as  
> > well as all the other class, BUT Gps (that uses firebird). When I  
> > request that page, the system tries to connect to firebird, but that  
> > tables are in postgres.
> 
> I think it should be sufficient to have the default in database.yml  
> point to the Postgres database and explicitly connect to Firebird  
> from the Gps class.

Perhaps a setup like this would be “better”:

class PostgresBase \< ActiveRecord::Base  
establish\_connection …  
end  
class FirebirdBase \< ActiveRecord::Base  
establish\_connection …  
end

class Driver \< PostgresBase  
…  
end

class Vehicle \< PostgresBase  
…  
end

class Gps \< FirebirdBase  
…  
end

That way if your app needs to call on more models from either  
database, you only establish\_connection() once for both.

---

<div class="post-metadata">

**Author:** ![Marcia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Marcia](https://www.ruby-forum.com/u/Marcia)\
**Post date:** [March 18, 2006, 12:05am UTC](https://www.ruby-forum.com/t/multiple-databases-switching-databases/55106/6 "2006-03-18T00:05:03Z")

</div>

Hi, people

The point to the Postgres database and explicity connect to Firebird  
from the Gps class was not sufficient, the system was lost. But thanks  
anyway. However, using this:

> class PostgresBase \< ActiveRecord::Base  
> establish\_connection …  
> end …

was effective. It works very well!

Thanks a lot!!!

Marcia

unknown wrote:

> > > In a page, I have two combos (one of them listing all vehicles and the  
> > > other, all drivers); to populate these combos, I use Vehicle.find\_all  
> > > and Driver.find\_all. Both model classes must use Postgres connection as  
> > > well as all the other class, BUT Gps (that uses firebird). When I  
> > > request that page, the system tries to connect to firebird, but that  
> > > tables are in postgres.
> > 
> > I think it should be sufficient to have the default in database.yml  
> > point to the Postgres database and explicitly connect to Firebird  
> > from the Gps class.
> 
> Perhaps a setup like this would be “better”:
> 
> class PostgresBase \< ActiveRecord::Base  
> establish\_connection …  
> end  
> class FirebirdBase \< ActiveRecord::Base  
> establish\_connection …  
> end
> 
> class Driver \< PostgresBase  
> …  
> end
> 
> class Vehicle \< PostgresBase  
> …  
> end
> 
> class Gps \< FirebirdBase  
> …  
> end
> 
> That way if your app needs to call on more models from either  
> database, you only establish\_connection() once for both.
