Rails: has_many association with a table in another database and without foreign key

Posted by Fernando on Stack Overflow See other posts from Stack Overflow or by Fernando
Published on 2012-06-10T22:35:44Z Indexed on 2012/06/10 22:40 UTC
Read the original article Hit count: 196

Here is my situation. I have model called Account. An account can have one or more contracts. The problem is that i'm dealing with a legacy application and each account's contracts are stored in a different database.

Example:

Account 1's contract are in account1_db.contracts. Account 2's contract are in account2_db.contracts.

The database name is a field stored in accounts table.

How can i make rails association work with this?

This is a legacy PHP application and i simply can't change it to store everything in one table. I need to make it work somehow.

I tried this, but it didn't worked:

has_many :contracts, :conditions => [lambda{ Contract.set_table_name(self.database + '.contracts'); return '1' }]

Any ideas?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3