我正在尝试在rails 5.0.0.beta1
. 一切似乎都很好,但我遇到了一些奇怪的事情。模型连接到指定的数据库,关联工作,迁移(经过一些调整)也工作正常。active_record_internal_metadatas
运行迁移后,两个数据库中都会出现一个名为 的附加表。当我在上面测试这个设置时rails 4.2.5
,没有这样的东西。有人可以阐明这个问题吗?
双数据库设置的设置:
database.yml
default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: socket: /tmp/mysql.sock development: <<: *default database: app_name_development_first development_second: <<: *default database: app_name_development_second
second_db_connection.rb
- 处理与第二个数据库的连接的模型。应该在第二个数据库中的所有模型都必须继承自该模型。class SecondDbConnection < ActiveRecord::Base establish_connection "#{Rails.env}_second".to_sym self.abstract_class = true end
application_controller.rb
- 缓存第二个数据库的连接class ApplicationController < ActionController::API around_filter :cache_other_db_connections private def cache_other_db_connections SecondDbConnection.connection.cache { yield } end end
db.rake
- 覆盖迁移任务namespace :db do task :migrate do Rake::Task['db:migrate_first'].invoke Rake::Task['db:migrate_second'].invoke end task :migrate_first do ActiveRecord::Base.establish_connection ("#{Rails.env}".to_sym) ActiveRecord::Migrator.migrate('db/db_first/migrate/') ActiveRecord::Base.connection.close end task :migrate_second do ActiveRecord::Base.establish_connection ("#{Rails.env}_second".to_sym) ActiveRecord::Migrator.migrate('db/db_second/migrate/') ActiveRecord::Base.connection.close end end