The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Saturday, November 1, 2008

Calling rake tasks from another rake task

This turns out to be pretty straight forward, but was not obvious

Rake::Task['task_name'].execute

A rake task to rebuild the development database
desc 'drop, create and rebuild development db'
task(:rebuild_development_db) do
  puts "drop the db"
  Rake::Task['db:drop'].execute 
  puts "create the db"
  Rake::Task['db:create'].execute
  puts "run the migrations"
  Rake::Task['db:migrate'].execute
  # do other stuff...
end

Resources

2 comments:

Anonymous said...

Awesome! Thanks!

House 9 said...

Also if the task you are calling takes args you can pass them in a hash to the execute method

some_value = "testing"
Rake::Task["your_task"].execute({:some_param => some_value})

read more about rake args
http://dev.nuclearrooster.com/2009/01/05/rake-task-with-arguments/