[ruby on rails]同时链接多数据库

  • database.yml
default: &default
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  wait_timeout: 60
  
oracle_development: #名字可自定义,但是不能重复。
  <<: *default
  adapter: oracle_enhanced
  host: xxx.xxx.xxx.xx
  username: root
  password: 123456
  database: oracle_dev
  port: 1521 # Oracle 的端口号,Mysql为3306,mysql一般不写 

mysql_development:
  <<: *default
  adapter: mysql2
  host: xxx.xxx.xxx.xx
  username: root
  password: 123456
  database: mysql_dev

第一种使用方法

  • 新建app/other_database_models/other_application.rb
class OtherApplication < ActiveRecord::Base #OtherApplication只是个名字,可随便定义
  self.abstract_class = true
  establish_connection :oracle_development # database.yml中配置的数据库
end
  • 新建app/other_database_models/user.rb
  • 使用oracle_development中的users表
class User < Oth

猜你喜欢

转载自blog.csdn.net/qq_41037744/article/details/106214098
今日推荐