# Dynamically connecting to multiple databases

**URL:** <https://www.ruby-forum.com/t/dynamically-connecting-to-multiple-databases/179773>\
**Category:** Rails\
**Created:** [December 3, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/dynamically-connecting-to-multiple-databases/179773 "2009-12-03T14:59:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [December 3, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/dynamically-connecting-to-multiple-databases/179773/1 "2009-12-03T14:59:13Z")

</div>

Hi All,

In my app after logged in there 'll be 3 links like

DEMO (set value for this link like D)  
DEMO1 (set value for this link like D1)  
DEMO2 (set value for this link like D2)

Each link refers to different database, based on the clicked links i  
need to show the results for that i’m storing link value in  
session[:gateway],and these 3 databases 've same tables eg: users  
table,exists in all the 3 databases only data varies. inorder to achieve  
this i did the following:

===============================================================  
#custom\_config.rb this is saved in lib folder  
require ‘active\_record’  
$config = YAML.load\_file(File.join(File.dirname( **FILE** ),  
‘database.yml’))  
class DEMO \< ActiveRecord::Base  
establish\_connection $config[‘demo’]  
end  
class DEMO1 \< ActiveRecord::Base  
establish\_connection $config[‘demo1’]  
end  
class DEMO2 \< ActiveRecord::Base  
establish\_connection $config[‘demo2’]  
end

================================================================  
#database.yml file, ##host is same for all the databases.

demo1:  
adapter: oracle\_enhanced  
database: ADEMO  
port: 1521  
username: demo1\_user  
password: demo1\_pass

demo2:  
adapter: oracle\_enhanced  
database: BDEMO  
port: 1521  
username: demo2\_user  
password: demo2\_pass

demo:  
adapter: oracle\_enhanced  
database: CDEMO  
port: 1521  
username: demo\_user  
password: demo\_pass

============================================================  
#User Model, this model is same for all the databases.

require ‘custom\_config’  
module UserDEMO  
class User \< DEMO  
has\_many :scheduleddowntimes  
#This method is just an example.  
def self.authenticate(user\_name,password)  
user=User.find(:first, :conditions=\>[“loginname=? and passwd=?”,  
user\_name,password])  
if user && user.instance\_of?(User)  
if user.passwd!=password and user.status==“D”  
user=nil  
end  
return user  
end  
end  
end  
end

module UserDEMO1  
class User \< DEMO1  
has\_many :scheduleddowntimes  
end  
end

module UserDEMO2  
class User \< DEMO2  
has\_many :scheduleddowntimes  
end  
end

==================================================================

#In Controller

# session[:gateway] stores the values( like D, D1, D2)when clicked on

the links #for choosing database.  
modules=%w{UserDEMO UserDEMO1 UserDEMO2}  
gateways=%w{D D1 D2}  
users=Login.find(session[:user\_id])  
login\_user=modules[gateways.index(session[:gateway])]::User.authenticate(users.username,user.password)  
if gateways.include? session[:gateway]

====================================================================  
The above line throwing an exception " UserDEMO is not a  
class/module" (if i click on the link DEMO and same for remaining 2  
links it throws " UserDEMO1 is not a class/module"," UserDEMO2 is not  
a class/module"

I thought this might be a problem with modules array, so i tried simply

UserDEMO::User.authenticate(users.username,user.password) if  
gateways.include? session[:gateway]

But still same exception.

So can anyone please help where i’m going wrong?

Thanks  
VK
