3
My Rails 3.2應用程序使用兩個不同的數據庫。主OS是一個MySQL而oher是一個SQLServer數據庫:。使用兩個數據庫預裝
development:
adapter: mysql2
encoding: utf8
database: mydb
username: myuser
password: ***
secondbase:
adapter: sqlserver
database: anotherbase
username: anotheruser
password: ***
我有兩個相關機型:
class Account < ActiveRecord::Base
establish_connection :secondbase
has_many :docs
end
class Docs < ActiveRecord::Base
belongs_to :account
end
由於兩型動物數據庫兩種模式使用表,我想我可以」 T選用eager_loading和負載相關的數據與join
或includes
但我認爲preload
應該還是讓我來加載使用兩個查詢的數據(而不是N + 1)
然而,試圖在我的控制器使用preload
時,它提出了一個錯誤:
Docs.where(someconditions).preload(:account)
回報
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]
我能做什麼預加載的帳戶數據,避免N + 1個查詢時dusplaying的文檔與一些帳戶細節在我的意見?
嗯......呃,這不一定是Rails設計的目的。當然這是可能的,但是爲了使用AR的魔力,你可能希望看到有一個數據庫到達Rails之後。檢查是否有設計用來做這件事的寶石。這是一個較舊的線程上有一些很好的鏈接:http://stackoverflow.com/questions/1825844/multiple-databases-in-rails – 2012-03-19 02:10:04
我知道,Rails是不是旨在處理多個數據庫。在我的模型中需要時使用'establish_connection'對於我的大多數需求來說足夠好。我只想通過熱切/預加載來提高性能。 – LapinLove404 2012-03-21 09:51:17