[英]Rails connect to remote database without model
What is the best way to connect to a remote database from Rails just to pull some data? 从Rails连接远程数据库只是为了提取一些数据的最佳方法是什么? I need to execute a query on the remote server and retrieve the column values.
我需要在远程服务器上执行查询并检索列值。 These columns will be the stored locally within a model.
这些列将存储在模型中的本地。
Thanks! 谢谢!
For multiple database connection, you need to add the following codes to the database.yml file. 对于多数据库连接,您需要将以下代码添加到database.yml文件中。
config/database.yml 配置/ database.yml的
other_db:
adapter: mysql2
database: db1_dev
username: root
password: xyz
host: localhost
Then create a new model. 然后创建一个新模型。
class ImportLine < ActiveRecord::Base
establish_connection "other_db"
self.table_name = "the_table_in_th_other_db"
end
Now you can select arbitrary columns like this: 现在您可以像这样选择任意列:
ImportLine.select(:col1, :col2).find_each do |line|
puts "#{line.col1} -#{line.col1}"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.