Forum: Ruby on Rails Connecting to multiple databases

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Martin G. (Guest)
on 2006-04-18 04:33
(Received via mailing list)
Hi Everyone,

I am trying to connect to multiple databases and followed along the
Recipe in Chad F.s 'Rails Recipes' book (which basically is about
establishing the connection in a subclass of ActiveRecord::Base, and
inheriting all classes in need of this connection from this class)

Chad F. says:
"You won't be able to instantiate an External, of course, since there is
matching database table. If there is a table in your external database
called externals, choose a different name for your class to be on the
side." ===> I dont have such a table

Dave T. says at

"In general, you'd expect the LegacyBase class to map to a database
table called legacy_base. It would, if we ever tried to use it to
access data. But because we don't, and because Rails only reflects on
the table the first time a data access occurs, we can safely create an
ActiveRecord class with no underlying database table." ===> I am not
accessing this data on purpose :-)

However, when I do things according to these descriptions, I end up
with rails showing me an error message about a missing table (named
after the common connection baseclass)

One reason I could think of is that I instantiate an object of the
model class inheriting the connection. I think that maybe during
initialisation of the new object (chained constructor calls down the
inheritance hierarchy) rails IS in fact trying to access this table?
Then again I think the howto for working with multiple databases would
be pretty useless, if one weren't able to instantiate a new object via
such a shared connection (i.e. inserting rows)?

Any ideas more than welcome!"!!!

Detailed error message and code below


Mysql::Error: #42S02Table 'ca_sandbox.web_ca_admin_sandbox_bases'
doesn't exist: SHOW FIELDS FROM web_ca_admin_sandbox_bases


class WebCaAdminSandboxBase < ActiveRecord::Base
	establish_connection "webcaadmin_sandbox_#{RAILS_ENV}"

class Community < WebCaAdminSandboxBase

	# This causes the security features to be added to the model.
	include ModelSecurity

	belongs_to 	:user
	has_many	:communal_audits

	def initialize




class WebadminController < ApplicationController

	#scaffold	:community

	helper		:ModelSecurity

	model		:year,


	def new_community

		@community =
                # ....



Stephen B. (Guest)
on 2006-04-18 09:26
(Received via mailing list)
Hi Martin,

Perhaps this might be helpful -- here's how I access two different

file: database.yml

   adapter: mysql
   host: localhost
   database: myapp_development
   username: root
   password: ********

   adapter: mysql
   database: external
   username: rails
   password: ********


Here's a regular model that access my normal rails databases:
file: activity.rb:

class Activity < ActiveRecord::Base

Here'a a model I derive from for accessing the legacy db external:
file: external_model.rb

class ExternalModel < ActiveRecord::Base
   establish_connection :external

Here's a specific model that derives from ExternalModel
file: external_user.rb

class ExternalUser < ExternalModel
   set_table_name "user_table"
   set_primary_key "user_id"

So now I can make as many derived classes from ExternalModel for
accessing tables in the legacy db 'external' as I need. I am also
setting table_name and primary_key because the legacy db doesn't use
rails naming conventions.
This topic is locked and can not be replied to.