views:

72

answers:

1

For legacy reasons, my model's tables are stored in two different databases on the same MySQL server. At first I just used establish_connection to specify the second database on the models that used it. However, when I established a has_many :through connection between two models in different databases, it blew up because MySQL cannot JOIN across two different servers. Then I noticed on the interwebs that I could use set_table_name 'other_database.foos' instead of using establish_connection.

This mostly works, but table_exists? always returns false even when the table does exist, and a couple of the plugins I'm using use that function extensively.

Is this a bug in table_exists?, or am I misusing set_table_name? Is there another way of doing what I'm trying to do?

+1  A: 

table_exists? does the following:

def table_exists?(table_name)
  tables.include?(table_name.to_s)
end

Where tables is an array of the table names for the established connection (defined in databases.yml). So if you call table_exists?('database.table_name') or table_exists?('table_name_from_second_database') then it will always return false.

The only way around this would be to monkey patch table_exists? or the function that loads the tables array. See the api docs to see where to start with the patching.

Good Luck!

Tony Fontenot