setting up has_many_through association between different db

I am trying to setup a has_many :through relationship between two models User and CustomerAccount through another join model AccountOwnership (the underlying tables which BELONG to two DIFFERENT DATABASES).

Here is the relevant code

Solution:

Seems that this cannot be achieved through any Rails association magic, since this is a core limitation of any database access mechanism including raw SQL.

Here is what I did to workaround the issue:

class User < ActiveRecord::Base   has_many :account_ownerships, :dependent => :destroy

  def companies     (account_ownerships.collect { |r| Reporting::CustomerAccount.find(r.company_id) }).flatten   end end

This provides a correct approximation as shown:

a = AcccountOwnership.create!(:user_id => 10, :company_id => 10) u = User.find(10) u.account_ownerships ### will return the correct account_ownership instance

ALSO

u.companies ### will return a list of all companies enlisted for each account

And we need to add two instance methods to the account_ownership model, to approximate the association behavior

class CustomerAccount < ActiveRecord::Base   set_table_name "customers"