We are creating a reporting/monitoring solution that will connect to multiple databases which are not rails compliant. That being said, the data being gathered is quite simple. BUT we won't know what the connection details are until we access those details in another database record... confusing enough? Connection details are basically provided by a third party, so they have to be dynamic.
Am I doing this the wrong way? Why does it work on localhost, but not when opening a remote connection? (Something about the Name not being set...? )
Here is what I want to do:
The table reporting_sites is on my localhost reporter_production database. It has a field that contains the site name and database connection details for that database. For example, :db_name => "FunActivities", :conn_details => { :host => 'mydb.mysite.com', :username => 'user1', :password => 'mypwd', :table_name => 'activities', etc. etc.. etc... }
My class looks like this...
class ReportingSite < ActiveRecord::Base validates_...
def after_initialize self.conn = Class.new(ActiveRecord::Base) self.conn.establish_connection( :adapter => "mysql", :host => self.conn_details[:host], :database => self.conn_details[:database], :username => self.conn_details[:user], :password => self.conn_details[:password] ) self.conn.set_table_name self.conn_details[:table_name] self.conn.set_primary_key self.conn_details[:primary_key] end end
It works every time when I am connecting to the local host and no other connection details (testing basically). But when I need to connect to another database, self.conn is created and I get this information in script/console
s1 = ReportingSite.find(1)
=> #<ReportingSite:0x... ALL DETAILS HERE>
s1.conn
=> #<Class:0x...>
s1.conn.methods
=> ALL ACTIVERECORD METHODS
s1.connection
=> #<ActiveRecord::ConnectionAdapters::... ALL CONNECTION INFO FOR ReportingSite model>
s1.conn.connection
=> NameError: "" is not a valid constant name!