我正在尝试将我的用户帐户和会话数据移动到一个单独的数据库中,以便我们最终能够在多个应用程序之间共享它。
我在网上见过很多人说要用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: authenticationapp/models/user.rb
class User < ActiveRecord::Base
  establish_connection :authentication
  has_one :person
endapp/models/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似乎在使用当前数据库,而不是通过反射获得正确的数据库。
我发现了大约两年前关于这个问题的这个很旧的错误报告,但是我几乎可以肯定它是关于旧的ARel语法的,而且我真的很怀疑代码示例是否能继续工作。
这有可能吗?
Update:通过这样做取得了一些进展:
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实际上并不支持多个模式。我说错了吗?
发布于 2011-01-20 22:20:55
您是否考虑过将其从应用程序层中删除,只使用MySQL复制来复制某些表?
https://stackoverflow.com/questions/4372429
复制相似问题