How to pass arguments to a Rake task

Photo by Lukas on Unsplash

Update sep/2018 — I posted this same text on my personal blog and would be very happy if you could read it there. Thanks!

There are many ways how to pass an argument to a rake task, some of these are covered in at this blog post.

IMHO, the most elegant way to do this is using the first way described at post, the Rake Way:

And, you run it…

All right! Nothing to do here! YAY!

A common task block is like this:

The :environment symbol is what tells to rake task to load your entire Rails environment. So, without this you can't access your models, for example.

To using arguments in a elegant way and load your Rails environment in your Rake task, you can write your task method like this:

And, this is the output:

Thanks! 😄

References: