2
我需要從外部數據庫(不是主數據庫)獲取一些數據。所以我在database.yml中添加了一個連接條目。Rails - 如何從外部數據庫訪問表
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
我有這個模式,我需要從外部數據庫中的數據,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
我應該怎麼做才能從custom_report.rb中的外部數據庫訪問表?
謝謝,我知道這種方法,我正在尋找任何其他方式來處理外部數據庫表,而無需創建其他模型。 –
@AlbertPaul這是Rails的方式。你爲什麼不想使用ActiveRecord? –
你是對的,謝謝。 –