114

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
zishe
  • 10,665
  • 12
  • 64
  • 103
Rails beginner
  • 14,321
  • 35
  • 137
  • 257

7 Answers7

157

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
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
30
Rake::Task['reklamer:orville'].invoke

or

Rake::Task['reklamer:orville'].invoke(args)
Luke W
  • 8,276
  • 5
  • 44
  • 36
  • This approach will work, but the "correct" way to bundle up raks tasks was answered by @andrew-marshall. – Tom Harrison Oct 15 '12 at 14:17
  • 4
    please provide reference if you're going to say something like 'the "correct" way...'. my answer was a response to the original question. – Luke W Oct 24 '12 at 19:32
  • 3
    As a theoretical test, I tried running `Rake::Task['db:seed'].invoke` but it does not run, saying "Don't know how to build task 'db:seed'" – lulalala Sep 04 '13 at 07:40
  • See this [answer](https://stackoverflow.com/a/49400110/7037546) if you get the error "_Don't know how to build task_" – dlauzon Mar 10 '22 at 21:21
28

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
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Bloomberg
  • 2,317
  • 2
  • 25
  • 47
  • Those quotes are bad but I can't edit them because it is too small an edit! – nroose May 20 '18 at 00:53
  • 2
    Even though this is not a real answer to the question asked. This is what I have been looking for and solved my problem (YourApp::Application.load_tasks). Thanks! – Dennis van de Hoef - Xiotin Mar 13 '20 at 09:51
  • 2
    `Rails.application.load_tasks` (instead of `YourApp::Application.load_tasks`) can also be used, to be slightly more generic. – dlauzon Mar 10 '22 at 21:18
24

Have you tried rake reklamer:iqmedier ?

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

CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
15

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.

Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
3

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.

TorvaldsDB
  • 766
  • 9
  • 8
1

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
    
hprakash
  • 452
  • 2
  • 10