Rake Tasks in Rails

Rake tasks don’t have to be difficult, but no one will ever tell you how to do them. Cuz fuck, why would any rails application need a rake task?

# lib/tasks/grab_todays_rates.rake
namespace :grab_todays_rates do
  task :save_rates => :environment do
    Rate.save_rates
  end
end

This is run with $ rake grab_todays_rates:save_rates

In Rails 5 rails grab_todays_rates:save_rates

On heroku: heroku run rails grab_todays_rates:save_rates

Then you can schedule it with scheduler.

Post Content