# Multiple database connections

**URL:** <https://www.ruby-forum.com/t/multiple-database-connections/139187>\
**Category:** Rails\
**Created:** [June 1, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/multiple-database-connections/139187 "2008-06-01T12:32:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [June 1, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/multiple-database-connections/139187/1 "2008-06-01T12:32:22Z")

</div>

Hi,

I’m trying to connect to a second database, but I can’t seem to get it  
working.

When a new user gets created in the application, the user data gets  
writter to the application database into the users table like all normal  
applications. After the user gets created, the application needs to  
connect to a second database. The second database is the pureftpd  
database where ftp user data gets stored.

The problem is, the pureftpd database has a table called users, and so  
does my application, so creating a new model “user” like I have read in  
all the sites that give a small tutorial on multiple database  
connections is out of the question.

I also tryed using ruby-sequel to handle the second database connection,  
but that conflicts with ActiveRecord.

I have never set u multiple connection in one application, so I have no  
idea on what doing next, and I was hoping someone could help me out on  
this one.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [June 1, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/multiple-database-connections/139187/2 "2008-06-01T12:46:40Z")

</div>

On Jun 1, 11:32 am, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The problem is, the pureftpd database has a table called users, and so  
> does my application, so creating a new model “user” like I have read in  
> all the sites that give a small tutorial on multiple database  
> connections is out of the question.

The following should work (but I haven’t tried it , so it might not).  
Create a second class called PureFtpUser (or something like that). In  
that model, use set\_table\_name to set the table used to users and use  
establish\_connection to tell it to connect to another database. If you  
don’t want to pollute your code with the connection details,  
ActiveRecord::Base.configurations contains all the configurations  
defined in your database.yml, so you can do something like  
establish\_connection(ActiveRecord::Base.configurations[“pureftp”]

Fred

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [June 1, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/multiple-database-connections/139187/3 "2008-06-01T14:41:40Z")

</div>

Thank you for your quick reply. Everything seems to work as you  
described, except for the configurations[] part. When I specify the  
whole string, it works. But that is something I’ll check out later.

Thank you again for the big help
