Just recently, I learned how to write my own rake tasks. My co-worker, Matt Heidemann, was nice enough to walk me through it after work one day. Everyone who is reading this probably already knows how to do this, but I figured I'd write about it anyway just for fun. My first rake task was very simple and just used system calls to tar and gzip a folder in my /vendor directory. Here's what I did to write it. (Note: the names of my folders and tasks have been changed to protect the innocent.)

First I created the rake file in my /lib/tasks directory, let's call it mytasks.rake. The first thing I want to do is describe what my rake task is going to do. Do this by adding desc before the task. Next, define the task with task :task_name and a block containing what the task will do.

Wow, that was easy. Now this task can be run by calling rake folder_zip at the command line. Sweet.

A couple things to note:
In order to access your models in a rake task, just add "=> :environment" after the task name; like this:

You can add a namespace block around your tasks if they fit into one category. So then you can call your tasks like this: rake folder:zip, rake folder:someothertask, etc.

So that was pretty darn exciting for me, since rake tasks used to be a such a mystery. There is a nice rake tutorial here that gives a much better explanation than my simple overview.

1 Comment on “Rake”

Josh Knowles Says:

As opposed to using images for your code samples you can use the built in syntax highlighter. All you need to do is install the syntax.css stylesheet, then surround your code with:


June 18th, 2007

Sorry, comments are closed for this article.

Lindsay Ucci (aka Ooochie!)

Lindsay Ucci

Ruby/Rails developer and wannabe pastry chef. :)

Welcome to ooochie.com! I'm Lindsay Ucci, and I'm a web developer in Boston, MA. If you're in the area, please get in touch!

Design by Dan Ritz, FeedBot by Matt Forsythe, and Powered by Mephisto