I am trying to setup a has_many :through relationship between two models User and CustomerAccount through another join model AccountOwnership (users and account_ownerships tables are in one db, say db1 and the customer_accounts table is in remote db, say db2).
Here is the relevant code, that sets up the associations
class User < ActiveRecord::Base
  has_many :account_ownerships, :dependent => :destroy
  has_many :companies, :through => :account_ownerships
end
class AccountOwnership < ActiveRecord::Base
  belongs_to :user
  belongs_to :company, :class_name => "Reporting::CustomerAccount"
end
class CustomerAccount < Reporting::Base
  set_table_name 'customers'
  establish_connection("db2_#{RAILS_ENV}")
end
config/database.yml (configuration is correct, although not shown here)
development:
  reconnect: false
  database: db1
  pool: 5
db2_development:
  reconnect: false
  database: db2
  host: different.host
  pool: 5
In script/console
a = AccountOwnership.new(:user_id => 2, :company_id => 10)
a.user ## Returns the correct user
a.company ## returns the correct CustomerAccount instance
also
a.user.account_ownership ## returns a as anticipated
but
a.user.companies ## produces the following error:
#ActiveRecord::StatementInvalid: Mysql::Error: Table #'db2.account_ownerships' doesn't exist: SELECT `customers`.* FROM #`customers` INNER JOIN `account_ownerships` ON `customers`.id = #`account_ownerships`.company_id WHERE ((`account_ownerships`.user_id = 4))
The issue here is that the "account_ownerships" and "users" tables are contained in one default database (say db1), and the "customers" table is contained in a different database (say db2). The connections to the databases are configured properly, but during the lookup since there is only one database connection object available, Rails tries to find the account_ownerships database in db2 and hence fails.
It looks like my design/logic might be flawed because I cannot see a way to connect to two different databases using the same db connection, but I would be thrilled to see if there is a workaround, without changing the design. (I am reluctant to change the design because db2 is not under my control)
It looks like I can workaround this issue by moving my account_ownerships table to db2, but this less than ideal for me atleast.
Are there any alternate mechanisms/schemas to setup this association in Rails.
Thanks in advance. M