Connecting to multiple databases


#1

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
no
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
safe
side.” ===> I dont have such a table

Dave T. says at
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc

“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 :slight_smile:

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

ERROR

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

CODE

class WebCaAdminSandboxBase < ActiveRecord::Base
establish_connection “webcaadmin_sandbox_#{RAILS_ENV}”
end

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

	super
            #...

end

end

class WebadminController < ApplicationController

#scaffold	:community

helper		:ModelSecurity

model		:year,
			:community,
			:communal_audit,
			:ca_module,
			:ca_figure,
			:ca_question

public

def new_community

	@community = Community.new
            # ....

end

end

cheers
gamsl


#2

Hi Martin,

Perhaps this might be helpful – here’s how I access two different
databases:

file: database.yml

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

external:
adapter: mysql
host: database.host.com
database: external
username: rails
password: ********


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

class Activity < ActiveRecord::Base
end

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

class ExternalModel < ActiveRecord::Base
establish_connection :external
end

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”
end

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.