2010-12-07 40 views
5

我試圖將我的用戶帳戶和會話數據移動到單獨的數據庫中,以便我們最終可以跨多個應用程序共享它。establish_connection似乎不支持連接

我見過很多人在網上說使用establish_connection來告訴模型連接到不同的數據庫,但我無法得到它的工作。

的config/database.yml的

development: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: true 
    pool: 5 
    host: localhost 
    database: project_name_development 

authentication: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: true 
    pool: 5 
    host: localhost 
    database: authentication 

應用程序/模型/ user.rb

class User < ActiveRecord::Base 
    establish_connection :authentication 
    has_one :person 
end 

應用程序/模型/ person.rb

class Person < ActiveRecord::Base 
    belongs_to :user 
end 

這多少看起來工作:

> User.connection.instance_eval { @config[:database] } 
=> "authentication" 
> Person.connection.instance_eval { @config[:database] } 
=> "project_name_development" 

,我可以在隔離查詢User

> User.where(:admin => true) 
=> [ ... lots of results .. ] 

但只要我嘗試使用join它打破:

> User.joins(:person) 
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'authentication.people' doesn't exist: SELECT `users`.* FROM `users` INNER JOIN `people` ON `people`.`user_id` = `users`.`id` 

AREL似乎正在使用當前的數據庫,而不是通過反射來獲取正確的數據庫。

我從兩年前就發現了this very old bug report關於這個問題,但我幾乎可以肯定它是關於舊的ARel語法的,我真的懷疑這些代碼的例子會工作了。

這甚至可能嗎?

更新:做這個做了一個小的進展:

User.joins("INNER JOIN project_name.people ON project_name.people.user_id = authentication.users.id") 

但那是很乏味,我想加入其中一個表是多態。

我嘗試添加:

set_table_name 'project_name.people' 

但返回

NoMethodError: undefined method `eq' for nil:NilClass 

在我看來那Rails3中實際上並不支持多種模式。我錯了嗎?

+0

當我嘗試將我的2.3站點移植到rails3時,我注意到了同樣的問題。看來Arel 2.0.6保持與原始數據庫的連接,並且在使用establish_connection更改數據庫時並不知道。 Arel HEAD似乎是一種改進,但我注意到其他錯誤。帶上軌3.0.4 ... – 2011-01-06 12:21:23

+0

我相當確信,Rails不支持。 – jschorr 2011-01-20 22:19:18

回答

1

你有沒有想過要把它從應用程序層拿出來,只是用MySQL複製來複制某些表?

相關問題