how to make rails external database calls?

后端 未结 2 1583
轻奢々
轻奢々 2020-12-24 09:03

So i\'d like to be able to add an external database to my config/database.yml Then model one table from it.

Is this possible? I haven\'t been able to

2条回答
  •  自闭症患者
    2020-12-24 09:53

    First, define the connection information in database.yml:

    my_external_db:
      adapter: mysql
      username: ...
      ....
    

    Then, create the model and connect it to the external db

    class MyExternalModel < ActiveRecord::Base
      establish_connection(:my_external_db)
      set_table_name 'my_external_table'
    end
    

提交回复
热议问题