Ruby-on-rails – Using Rails Migration on different database than standard “production” or “development”


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_development: and quiz_production: definition pointing to a differnet host/db/user/password

My goal now is to define a Migration that uses "quiz_#{RAILS_ENV}`" as its database configuration.

What I have tried (and failed):

  • Setting ActiveRecord::Base.connection in the Migration file
  • Changing the db:migrate task in rails to set ActiveRecord::Base.connection there


How can I make rake db:migrate use that other database definition?


Best Solution

There's a much easier answer. Add this to your migration:

def connection

That's for Rails 3.1. For Rails 2.X or 3.0 it's a class function instead (eg def self.connection)