Running your Rake tasks requires two steps:
- Loading Rake
- Loading your Rake tasks
You are missing the second step.
Normally this is done in the Rakefile, but you have to do it manually here:
require 'rake'
Rails.application.load_tasks # <-- MISSING LINE
Rake::Task['my_task'].invoke