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中实际上并不支持多种模式。我错了吗?
当我尝试将我的2.3站点移植到rails3时,我注意到了同样的问题。看来Arel 2.0.6保持与原始数据库的连接,并且在使用establish_connection更改数据库时并不知道。 Arel HEAD似乎是一种改进,但我注意到其他错误。带上轨3.0.4 ... – 2011-01-06 12:21:23
我相当确信,Rails不支持。 – jschorr 2011-01-20 22:19:18