2

有關於軌道創建到多個數據庫的多個連接的許多問題和答案3.2多個數據庫連接:從外部計算器導軌和相同型號名稱

https://stackoverflow.com/a/7480330/2120023

https://stackoverflow.com/a/6305540/2120023

例子:http://ilikestuffblog.com/2012/09/21/establishing-a-connection-to-a-non-default-database-in-rails-3-2-2/

但我還沒有找到一個解決方案,使用兩個數據庫中出現的模型時工作。

如果我的默認數據庫有一個表titles和我的Other db有一個表titles如何訪問其他數據庫的Title模型?

title.rb

class Title < ActiveRecord::Base 
end 

othertitle.rb

class Other < ActiveRecord::Base 
    self.abstract_class = true 
    establish_connection "other_#{Rails.env}" 
end 

class OtherTitle < Other 
end 

,因爲我得到這個錯誤,我不能用上面(編輯:爲清楚起見沒有other_titles表或者只分貝titles表編輯2:如果我在Other數據庫中創建了other_titles表,一切正常,但這並不幫助我訪問titles表。):

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'other.other_titles' doesn't exist: SHOW FULL FIELDS FROM `other_titles` 

我也不能使用class Title < Other,因爲我得到一個TypeError: superclass mismatch for class Title錯誤。

database.yml

development: 
    adapter: mysql2 
    encoding: utf8 
    database: db_dev 
    pool: 5 
    username: xxxx 
    password: xxxx 
    socket: /var/lib/mysql/mysql.sock 

production: 
    adapter: mysql2 
    encoding: utf8 
    database: db 
    pool: 5 
    username: xxxx 
    password: xxxx 
    socket: /var/lib/mysql/mysql.sock 


other_development: 
    adapter: mysql2 
    encoding: utf8 
    database: other_dev 
    pool: 5 
    username: xxxx 
    password: xxxx 
    socket: /var/lib/mysql/mysql.sock 

other_production: 
    adapter: mysql2 
    encoding: utf8 
    database: other 
    pool: 5 
    username: xxxx 
    password: xxxx 
    socket: /var/lib/mysql/mysql.sock 
+0

您是否爲nasa _#{dev}的database.yml文件創建了適當的數據庫連接配置? –

+0

是的,但技術上的例子應該有'other_'而不是'nasa_'我將編輯它以顯示正確的代碼。 – paulguy

+0

發佈您的database.yml文件請。 –

回答