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

前端 未结 20 1433
借酒劲吻你
借酒劲吻你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 18:54

    Following on from @Bryan Larsen, if you're using an abstract Class to attach a series of models to a different database, and would like to migrate schemas on them, then you can do this:

    class CreatePosts < ActiveRecord::Migration
        def connection
          Post.connection
        end
        def up
          ...
        end
    end
    

    with a model set up something like:

    class Post < ReferenceData
    end
    

    and

    class ReferenceData < ActiveRecord::Base
      self.abstract_class = true
      establish_connection "reference_data_#{Rails.env}"
    end
    

提交回复
热议问题