Mysql Rails-如何从外部数据库访问表

Mysql Rails-如何从外部数据库访问表,mysql,ruby-on-rails,ruby,ruby-on-rails-3,Mysql,Ruby On Rails,Ruby,Ruby On Rails 3,我需要从外部数据库(不是主数据库)获取一些数据。所以我在database.yml中添加了一个连接条目 external_reporting_table: adapter: mysql2 encoding: utf8 database: reporting_db host: localhost username: root password: password 我还创建了一个类来解决它,external_reporting_db.rb class ExternalRepo

我需要从外部数据库(不是主数据库)获取一些数据。所以我在database.yml中添加了一个连接条目

external_reporting_table:
  adapter: mysql2
  encoding: utf8
  database: reporting_db
  host: localhost
  username: root
  password: password
我还创建了一个类来解决它,external_reporting_db.rb

class ExternalReportingDB < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :external_reporting_table
end
class ExternalReportingDB
我有这个模型,我需要从外部数据库custom_report.rb获取数据

class CustomReport < ExternalReportingDB
  def self.shop_data_collection_abstract(batch_selections)
    p "Here I need to get multiple data from external db's tables."
  end
end
class CustomReport

如何从custom_report.rb中的外部数据库访问表?

执行此操作时,我会根据ActiveRecord所期望的每个表的单个模型类来执行。例如,假设我的外部数据库有一个名为customers的表,那么我将定义一个名为“ExternalCustomers”的类,并在该类中设置build_连接和表名。下面是一个例子:

class ExternalCustomer < ActiveRecord::Base
  establish_connection :external_reporting_table
  table_name "customers"
end
如果不想为每个表添加新模型,可以通过连接查询外部数据库。例如:

ExternalReportingDB.connection.execute("select * from whatever...")

谢谢,我知道这种方法,我正在寻找任何其他方法来处理外部DB表,而不需要创建额外的模型。这是Rails的方法。你为什么不想使用ActiveRecord?
ExternalReportingDB.connection.execute("select * from whatever...")