Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run rake tasks from console?

Tags:

rake

console

I want to invoke my rake task from console. Is it doable? if yes, how to do so?

I tried this on console:

require 'rake' Rake::Task['my_task'].invoke 

but it give me this error:

RuntimeError: Don't know how to build task 

it's like the rake cannot found the task.

any help would be appreciated.

Thank you

Edit: I am using rails 2.3.5

like image 333
Fajarmf Avatar asked Jan 28 '11 10:01

Fajarmf


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

How do I see all rake tasks?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.

What does rake command do?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.


2 Answers

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 
like image 187
Daniel Rikowski Avatar answered Oct 31 '22 16:10

Daniel Rikowski


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.

like image 36
garno Avatar answered Oct 31 '22 17:10

garno