Rake task with arguments and :environment

Just a quick tip:

desc "Rake task with arguments and :environment"
task :task_name, [:arg1, :arg2] => :environment do |t, args|
  args.with_defaults(:arg1 => "Foo", :arg2 => "Bar")
  puts "Hello, #{args.arg1}. Bye, #{args.arg2}."
end

Notes:

  • You may omit the #with_defaults call.
  • You have to use an Array for your arguments, even if there is only one.

So you will be able to pass arguments to your rake task:

$ rake task_name["Moon","Sun"]
Hello, Moon. Bye, Sun.
This entry was posted in rails. Bookmark the permalink.

One Response to Rake task with arguments and :environment

  1. Bury says:

    Saved my life, thanks!