Using Rails Migration on different database than standard “production” or “development”

前端 未结 20 1460
借酒劲吻你
借酒劲吻你 2020-11-29 18:18

I have a rails project running that defines the standard production:, :development and :test DB-connections in config/database.yml

In addition I have a quiz_developm

20条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 19:02

    I've found a great clean way to do this:

    class CreateScores < ActiveRecord::Migration
    
      class ScoresDB < ActiveRecord::Base
        establish_connection("scores_#{Rails.env}")
      end
    
      def connection
        ScoresDB.connection
      end
    
      def up
        create_table :scores do |t|
          t.text :account_id
          t.text :offer
        end
      end
    
      def down
        drop_table :scores
      end
    end
    

提交回复
热议问题