1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/rails/run-a-rake-task-programmatically.md
2022-05-27 14:49:54 -06:00

738 B

Run A Rake Task Programmatically

Typically the way to run a rake task is with the rake command from the command line.

$ rake example:env

What if you have a rake task that you want to invoke as part of a Ruby script or from somewhere in your Rails codebase?

Your tasks can be called programmatically as well.

Consider these two rake tasks:

namespace :example do
  task :env do
    puts "Current Environment: #{Rails.env.upcase}"
  end

  task :message, [:msg] do |task, args|
    puts "Message: #{args[:msg]}"
  end
end

These can be called from somewhere else by referencing and invoking them like so.

Rake::Task["example:env"].invoke
Rake::Task["example:message"].invoke("Nice rake task!")