Skip to Main Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced content levelIntermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

4.2. What You Can Do for Rake

No matter how complete the predefined Rake tasks are, there are always going to be tasks specific to your project that you are going to want to automate. Because Rake is just Ruby, it's not hard to write new tasks. And as you might expect, Rails provides a couple of features to make it easy to integrate your custom Rake tasks with your entire Rails project.

The first is autoloading. Any file placed in the /lib/tasks directory and ending with the extension .rake will automatically be loaded when Rake is invoked. This means that you will be able to access your tasks from the same command line invocation of rake as the preexisting tasks, and also that your tasks will be included in the common listing for rake -T. Not only does this allow you to integrate your custom tasks, but it also facilitates keeping the rakefiles under control, because you can group related tasks into their own files.

The second feature is complete access to the Rails environment, including being able to use your ActiveRecord models in your Rake tasks and getting all the nice database connections for free.

4.2.1. A Simple Rake Task

To show you what a typical Rake task looks like, I chose a pretty basic one from the Rails distribution — the log:clear task, which resets all the log files. You can look at the source for all the Rails tasks in vendor/rails/railties/lib/tasks. Here's what log:clear looks like -- the task is named :clear inside a namepsace called :log:

namespace :log ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Rebecca Skinner, Stephen Klabnik, Ryan Bigg

Publisher Resources

ISBN: 9780470223888Purchase book