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