[英]Rails connect to remote database without model
從Rails連接遠程數據庫只是為了提取一些數據的最佳方法是什么? 我需要在遠程服務器上執行查詢並檢索列值。 這些列將存儲在模型中的本地。
謝謝!
對於多數據庫連接,您需要將以下代碼添加到database.yml文件中。
配置/ database.yml的
other_db:
adapter: mysql2
database: db1_dev
username: root
password: xyz
host: localhost
然后創建一個新模型。
class ImportLine < ActiveRecord::Base
establish_connection "other_db"
self.table_name = "the_table_in_th_other_db"
end
現在您可以像這樣選擇任意列:
ImportLine.select(:col1, :col2).find_each do |line|
puts "#{line.col1} -#{line.col1}"
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.