Rails how to run rake task

Ruby on-RailsRubyRake

Ruby on-Rails Problem Overview


How do I run this rake file in terminal/console?

my statistik.rake in lib/tasks

desc "Importer statistikker"
namespace :reklamer do
  task :iqmedier => :environment do
    ...
  end
  task :euroads => :environment do
    ...
  end
  task :mikkelsen => :environment do
    ...
  end
  task :orville => :environment do
    ...
  end
end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can run Rake tasks from your shell by running:

rake task_name

To run from from Ruby (e.g., in the Rails console or another Rake task):

Rake::Task['task_name'].invoke

To run multiple tasks in the same namespace with a single task, create the following new task in your namespace:

task :runall => [:iqmedier, :euroads, :mikkelsen, :orville] do
  # This will run after all those tasks have run
end

Solution 2 - Ruby on-Rails

Rake::Task['reklamer:orville'].invoke

or

Rake::Task['reklamer:orville'].invoke(args)

Solution 3 - Ruby on-Rails

Sometimes Your rake tasks doesn't get loaded in console, In that case you can try the following commands

require "rake"
YourApp::Application.load_tasks
Rake::Task["Namespace:task"].invoke

Solution 4 - Ruby on-Rails

Have you tried rake reklamer:iqmedier ?

My custom rake tasks are in the lib directory, not in lib/tasks. Not sure if that matters.

Solution 5 - Ruby on-Rails

If you aren't sure how to run a rake task, first find out first what tasks you have and it will also list the commands to run the tasks.

Run rake --tasks on the terminal.

It will list the tasks like the following:

rake gobble:dev:prime             
rake gobble:dev:reset_number_of_kits                                    
rake gobble:dev:scrub_prod_data

You can then run your task with: rake gobble:dev:prime as listed.

Solution 6 - Ruby on-Rails

In rails 4.2 the above methods didn't work.

  1. Go to the Terminal.

  2. Change the directory to the location where your rake file is present.

  3. run rake task_name.

  4. In the above case, run rake iqmedier - will run only iqmedir task.

  5. run rake euroads - will run only the euroads task.

  6. To Run all the tasks in that file assign the following inside the same file and run rake all

    task :all => [:iqmedier, :euroads, :mikkelsen, :orville ] do #This will print all the tasks o/p on the screen 
    end
    

Solution 7 - Ruby on-Rails

As the https://stackoverflow.com/a/5641807/7262646 and https://stackoverflow.com/a/49400110/7262646 described

you have to add

require 'rake'
Rake::Task.clear
YourAppName::Application.load_tasks

on the top of the file.

YourAppName comes from config/applicaion.rb, which is defined as a namespace, such as:

module YourAppName
  class Application < Rails::Application
  end
end

and then you can use

Rake::Task["task_name"].invoke

to invoke your task.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRails beginnerView Question on Stackoverflow
Solution 1 - Ruby on-RailsAndrew MarshallView Answer on Stackoverflow
Solution 2 - Ruby on-RailsLuke WView Answer on Stackoverflow
Solution 3 - Ruby on-RailsBloombergView Answer on Stackoverflow
Solution 4 - Ruby on-RailsCharlieMezakView Answer on Stackoverflow
Solution 5 - Ruby on-RailsKaka RutoView Answer on Stackoverflow
Solution 6 - Ruby on-RailshprakashView Answer on Stackoverflow
Solution 7 - Ruby on-RailsTorvaldsDBView Answer on Stackoverflow