My Rails 3.2 app use two different databases. The main os is a MySQL while the oher is a sqlserver DB :
development:
adapter: mysql2
encoding: utf8
database: mydb
username: myuser
password: ***
secondbase:
adapter: sqlserver
database: anotherbase
username: anotheruser
password: ***
I have two associated models :
class Account < ActiveRecord::Base
establish_connection :secondbase
has_many :docs
end
class Docs < ActiveRecord::Base
belongs_to :account
end
Since the two model use table on two differents database, I suppose I can't use eager_loading and load associated data with join
or includes
But I think preload
should still allow me to load the data using two queries (instead of N + 1)
However, when trying to use preload
in my controller, it raises an error :
Docs.where(someconditions).preload(:account)
returns
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USE [anotherbase]
What could I do to preload the Account data and avoid N + 1 queries when dusplaying the Docs with some Account details in my views ?