How to Use Multiple Databases in a Rails App Using the database.yml

前端 未结 1 891
难免孤独
难免孤独 2021-02-06 15:20

I\'ve read up on the documentation on how to do this, but in practice, I am having problems. In my app, I have 2 different databases as described below in my database.yml file.

1条回答
  •  误落风尘
    2021-02-06 15:39

    I tried to replicate and got it to work. It has to do with naming conventions: You're in development mode and AR will look for the development tag, which in your case does not exist for sqlite.

    Here is my database.yml:

    development:
      adapter: mysql2
      database: se_development
      username: root
      pool: 5
      timeout: 5000
    
    sqlite_development:
      adapter: sqlite3
      database: db/development.sqlite3
      pool: 5
      timeout: 5000
    

    Here is the model:

    class Plot < ActiveRecord::Base
      establish_connection 'sqlite_' + Rails.env
    end
    

    Worked for me. Hope this helps.

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