how to make rails external database calls?

后端 未结 2 1571
轻奢々
轻奢々 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
    
    0 讨论(0)
  • 2020-12-24 10:06

    set_table_name is removed, so instead, you should use: self.table_name

    0 讨论(0)
提交回复
热议问题