Ruby on rails 4 如何在Rails 4中迁移特定(非默认)数据库

Ruby on rails 4 如何在Rails 4中迁移特定(非默认)数据库,ruby-on-rails-4,rake,rails-migrations,Ruby On Rails 4,Rake,Rails Migrations,我尝试了中提供的答案,但没有成功。迁移只是针对我的默认数据库运行的 如何在Rails 4中迁移非默认db config/database.yml(用foo替换敏感值): db/migrate/20150107222716_create_personnels.rb: class CreatePersonnels < ActiveRecord::Migration def connection ActiveRecord::Base.establish_connection("qui

我尝试了中提供的答案,但没有成功。迁移只是针对我的默认数据库运行的

如何在Rails 4中迁移非默认db

config/database.yml(用foo替换敏感值):

db/migrate/20150107222716_create_personnels.rb:

class CreatePersonnels < ActiveRecord::Migration
  def connection
    ActiveRecord::Base.establish_connection("quiz_#{Rails.env}").connection
  end

  def change
    create_table :personnels do |t|
      t.string :fn
      t.string :ln

      t.timestamps
    end
  end
end
class CreatePersonnels
型号:

class Personnel < ActiveRecord::Base
  establish_connection "personnel_#{Rails.env}"
end
class人事
您可以使用它,请参阅这篇博文:

我们最终手动设置了一个包含所需架构和关联的数据库,并删除了迁移。然后,应用程序与问题中列出的模型和配置一起正常工作。肯定解决了这个问题,但不幸的是不是“Rails way”

class Personnel < ActiveRecord::Base
  establish_connection "personnel_#{Rails.env}"
end