My environment: Ruby 1.9.2p290, Rails 3.0.9 and RubyGem 1.8.8
unfortunately I have an issue when come across multiple database.
The situation is this: I have two model connect with two different
database and also establishing association between each other.
database connection specifying in each model, look likes
class Visit < ActiveRecord::Base
self.establish_connection “lab”
belongs_to :patient
end
class Patient < ActiveRecord::Base
self.establish_connection “main”
has_many :visits
end
I got an error when meet following scenario
@visits = Visit.joins(:patient)
Errors: Mysql2::Error: Table ‘lab.patients’ doesn’t exist: SELECT
visits
.* FROM visits
INNER JOIN patients
ON patients
.id
IS
NULL
Here ‘patients’ table is in ‘main’ database and ‘visits’ table in ‘lab’
database
I doubt when executing the code, that Rails is considering ‘patients’
table is part of ‘lab’ database [which holds ‘visits’ table].