Terminal 2014 bash 2014 85x19

Courtesy of Jade. :)

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.

Lindsay Ucci (aka Ooochie!)

Lindsay Ucci

Ruby/Rails developer and wannabe pastry chef. :)

Welcome to ooochie.com! I'm Lindsay Ucci, a web developer workin' my life away at Integrum Technologies, a rails consulting shop in Chandler, Arizona... and having tons of fun doing it.

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