NotDan
NotDan

Reputation: 32233

How do I override database connection info for a rake task in rails?

I have a rails rake task and I want it to ignore what is in database.yml and use something else. How do I do this?

Upvotes: 0

Views: 2050

Answers (2)

user1094125
user1094125

Reputation: 769

You can also have direct access to the database inside a rake task by using this format:

desc "Some task"
task SomeTask: :environment do
  Author.all.each do |author|
    ... some code
  end
end

Upvotes: 0

zetetic
zetetic

Reputation: 47568

You can use ActiveRecord::Base.establish_connection to set up a database connection within a Rake task, as described in this SO question.

Or create a separate environment and add the database configuration to database.yml. Then call the rake task with rake mytask RAILS_ENV=myenvironment

Upvotes: 2

Related Questions