require 'rake'
Rails.application.load_tasks # <-- MISSING LINE
Rake::Task['my_task'].invoke
$ rails g task my_namespace my_task1 my_task2
$ create lib/tasks/my_namespace.rake
$ bin/rake task_name
rake task_namespace:task_name
rails raketaskname:task