Today I Learned

hashrocket A Hashrocket project

Rails Generate Tasks

Andrew's TIL from the other day got me thinking about rails generators, and I never knew there was a generator for rake tasks!

There's not a ton of boilerplate in rake tasks, but that's never stopped me from making a typo.

bin/rails g task hello will generate just the namespace hello

# lib/tasks/hello.rake
namespace :hello do
end

If you have one or more tasks within the namespace, you can include them and the generator will add a task for each one:

bin/rails g task say hi bye good_day will generate

# lib/tasks/say.rake
namespace :say do
  desc "TODO"
  task hi: :environment do
  end

  desc "TODO"
  task bye: :environment do
  end

  desc "TODO"
  task good_day: :environment do
  end
end
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.