run rake task in rails console

require 'rake'
MyRailsApp::Application.load_tasks
Rake::Task['task_name'].invoke
  1. require 'rake': Imports the Rake gem, which is Ruby's build program.
  2. MyRailsApp::Application.load_tasks: Loads the tasks defined in the Rails application.
  3. Rake::Task['task_name'].invoke: Invokes a specific Rake task named 'task_name', executing its associated functionality within the Rails environment. Replace 'task_name' with the actual name of the task you want to execute.