Efficient way to pull data from second database?

前端 未结 3 911
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 14:41

We have a primary database where all of our app resides.

But there\'s a second database (updated from an external source), that I\'d like to be able to connect to so

3条回答
  •  一向
    一向 (楼主)
    2021-01-06 15:27

    For simple scenarios, Rails can support this without any extra gems; simply define the database in database.yml:

    other_db:
      adapter: mysql2
      encoding: utf8
      database: other_db
      username: user
      password: passwd
      host: 1.2.3.4
      port: 3306
    

    Then in the model you want to use the other database add:

    class Article < ActiveRecord::Base
      establish_connection(:other_db)
      self.table_name = 'other_db.articles'
    end
    

    And then you can perform your query:

    Article.where("id > 1000")
    

    =)

提交回复
热议问题