MySQL `SHOW FULL FIELDS` statement using wrong database

Hi Everyone,

I would like some help with a MySQL runtime error in my application. The
error is:

Mysql::Error: Table ‘urbmi5_drupal.country_stateprovince’ doesn’t exist:
SHOW FULL FIELDS FROM country_stateprovince

The problem with this query is that it is running on the wrong database.
Also, when I am in the rails console, I can query all the databases in
my
application without a problem.

So here’s a quick overview of my database setup… the application has 3
databases – the first is a standard rails database, and the other two
are
legacy databases included via a gem for reuseability. The credentials
for
all 3 databases are read in from database.yml. In any models associated
with the legacy database, I am calling establish_connection(…) with
the
appropriate key into the database.yml. I also tried setting the
“table_name_prefix” with the name of the correct database in the model
that
appears to be giving me trouble, e.g. self.table_name_prefix =
“techmi5_voleng.”, but that doesn’t help.

If that sounds too theoretical, here is some code, specifically from the
model that I think is failing to load:

module Techmi5Voleng
class Province < ActiveRecord::Base
include ChrisvolModelage::Techmi5Voleng::Connection

self.table_name = "country_stateprovince"
self.primary_key = "state_code"

def country
  Techmi5Voleng::Country.find(:first, :conditions => "UCASE(iso3) =

UCASE(’#{self.country_code}’)")
end
end
end

And for the database connection modules that it references (on the third
line):

module ChrisvolModelage
module Techmi5Voleng
module Connection
def self.included(model)
ChrisvolModelage::db_connect(model, :techmi5_voleng)
end
end
end
end

module ChrisvolModelage
def self.db_connect(model, yml_key)
yml_key = yml_key.to_s
yml_key += “_#{ENV[‘RAILS_ENV’]}” if ENV[‘RAILS_ENV’]

model.establish_connection(YAML.load(File.read(‘config/database.yml’))[yml_key])
end
end

One final comment is that it looks like the error is triggered as soon
as
the application references a model from the other legacy database (not
used by the model that is failing to load), urbmi5_drupal. I added the
urbmi5_drupal models to the gem first, and they worked fine
independently.
So it seems that when the application first loads an urbmi5_drupal
model,
it tries to load the techmi5_voleng models as well, but it has the wrong
database selected.

I am very stumped, so I would appreciate any help!

Best,
Jim