How to pass arguments to a Rake task
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:
: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:
Rake tasks are a great way to enhance (or even automate) your workflow with a ruby project. When I was first learning…cobwwweb.com
I am able to pass in arguments as follows: desc "Testing args" task: :hello, :user, :message do |t, args|…stackoverflow.c