How to run rake tasks from console?

Running your Rake tasks requires two steps:

  1. Loading Rake
  2. Loading your Rake tasks

You are missing the second step.

Normally this is done in the Rakefile, but you have to do it manually here:

require 'rake'
Rails.application.load_tasks # <-- MISSING LINE
Rake::Task['my_task'].invoke

The easy way is:

Rails.application.load_tasks
Rake::Task['my_task'].invoke

The easiest way to do it is to run %x[command] from the irb. I'm not sure if what you want to achieve though.

%x[rake db:migrate]

EDIT: I highly recommend to use .invoke as Daniel says in the accepted answer.

Tags:

Rake

Console