对于多数据库连接,您需要将以下代码添加到database.yml文件中.
配置/ database.yml的
other_db:
adapter: mysql2
database: db1_dev
username: root
password: xyz
host: localhost
Run Code Online (Sandbox Code Playgroud)
然后创建一个新模型.
class ImportLine < ActiveRecord::Base
establish_connection "other_db"
self.table_name = "the_table_in_th_other_db"
end
Run Code Online (Sandbox Code Playgroud)
现在您可以像这样选择任意列:
ImportLine.select(:col1, :col2).find_each do |line|
puts "#{line.col1} -#{line.col1}"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1874 次 |
| 最近记录: |