Establish a 2nd database connection in Rails
OK, you can find some howto’s everywhere on the web but only on this site I found the complete solution:
class SecondConnection < ActiveRecord::Base SecondConnection.establish_connection( :adapter => "informix", :host => "localhost", :username => "hansnase", :password => "very_secret", :database => "ccdb@localhost" ) self.abstract_class = true end
“self.abstract_class = true” makes the thing actually work. Otherwise Rails seems to cache the database connection and does not switch back if you access any other classes but SecondConnection.
Yeah!
Later: Not so Yeah…
I had to establish another call to
ActiveRecord::Base.establish_connection
logger.info { "Establishing Base Connection" }
log_connections
I assume it’s either an Informix problem or related to the old rails version I’m driving with.
Nevertheless: It works now.
