<p>I have a situation where I have two models, companies and permissions, where companies is in a separate database from my permissions database.  This is a has and belongs to many relationship because each company can have many permissions and each permission can belong to many companies.</p>

<p>The reason the two databases are split is because the company database runs a high demand production application and the permissions database controls the permissions for another application.</p>

<p>With rails, it looks for the join table in the same database as the primary table.  For instance, if I do company.permissions, it looks in the company database for company_permissions.  If I do permission.companies it looks in the permissions database.</p>

<p>What is the best solution to using a has and belongs to many relationship with multiple databases?</p>

If the companies data does not change very often, perhaps you could synchronize the data into the permissions database, then do your join naturally. A CMS I inherited is doing something like that where the user authentication is in its own database, and the permissions and such are all stored there, and user accounts are synch'd to the local database for each site. This way user accounts could be shared (especially for administration) across multiple sites with 1 login.
May not be the best because a synch is involved. Ideally you should just store the company data in the permissions db if you can't move permissions out. Unless of course you are joining on company elsewhere.