Find an article

Start typing to search the archive.

← All articles

An easy way to create your own Rake task

Learn how to generate and run custom Rake tasks in Ruby on Rails, with a complete example covering namespaces, task descriptions, and application loading.

Updated · Tested with Rails 8.1.0 and Ruby 3.4.9

How to generate a Rake task

TL;DR: from your Rails application’s root, run bin/rails generate task my_namespace my_task1 my_task2. Add code to the generated tasks, then execute one with bin/rails my_namespace:my_task1.

I’m Andrei Kaleshka, a Rails developer and the author of Rake Task Management Essentials. I originally wrote this article in 2012 after finding Rails’ task generator. Here is a complete example you can try in your own app.

Generate a Rake task

You can write a .rake file yourself, copy an existing task, or let Rails generate the skeleton:

bin/rails generate task greetings hello goodbye

The shorter form, bin/rails g task greetings hello goodbye, does the same thing. greetings is the namespace; hello and goodbye are task names. Rails creates lib/tasks/greetings.rake:

namespace :greetings do
  desc "TODO"
  task hello: :environment do
  end

  desc "TODO"
  task goodbye: :environment do
  end
end

Add code and run it

Replace the placeholders with descriptions and a little Ruby:

namespace :greetings do
  desc "Print a greeting"
  task hello: :environment do
    puts "Hello from #{Rails.application.class.module_parent_name}!"
  end

  desc "Print a farewell"
  task goodbye: :environment do
    puts "Goodbye!"
  end
end

Run the first task from the application root:

bin/rails greetings:hello

In an app named TaskDemo, this prints:

Hello from TaskDemo!

Run bin/rails greetings:goodbye to print Goodbye!. You can also execute these tasks with bundle exec rake greetings:hello or bundle exec rake greetings:goodbye.

What does :environment do?

task hello: :environment makes Rails’ environment task a prerequisite. It loads your application before the task body runs, so you can use application classes, models, and database queries. The greeting uses it to access Rails.application.

For a task that only uses plain Ruby, you can omit that prerequisite. For example, task :goodbye do is enough for the farewell above. Keep task bodies small: when the work grows, put the logic in an application class and call it from the task.

List your tasks

To list the tasks in this namespace:

bin/rails -T greetings

The list includes the descriptions you supplied:

bin/rails greetings:goodbye  # Print a farewell
bin/rails greetings:hello    # Print a greeting

-T lists tasks with a desc; an executable task without a description can be missing from this list. Use bundle exec rake -T -A greetings to include tasks without descriptions. Listing a task does not execute its body; run its full namespace:task name to do that.

See the Rails command-line guide and Rake command-line reference for more details.

Learning more

Rake Task Management Essentials

My book, Rake Task Management Essentials, covers Rake through practical examples. You can find it at Packt.