Rails how to run rake task
Olivia Zamora
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 7 Answers
You can run Rake tasks from your shell by running:
rake task_nameTo run from from Ruby (e.g., in the Rails console or another Rake task):
Rake::Task['task_name'].invokeTo 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 11 Rake::Task['reklamer:orville'].invokeor
Rake::Task['reklamer:orville'].invoke(args) 4 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 3 Have you tried rake reklamer:iqmedier ?
My custom rake tasks are in the lib directory, not in lib/tasks. Not sure if that matters.
1If 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_dataYou can then run your task with: rake gobble:dev:prime as listed.
As the and described
you have to add
require 'rake'
Rake::Task.clear
YourAppName::Application.load_taskson 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
endand then you can use
Rake::Task["task_name"].invoketo invoke your task.
In rails 4.2 the above methods didn't work.
- Go to the Terminal.
- Change the directory to the location where your rake file is present.
- run rake task_name.
- In the above case, run rake iqmedier - will run only iqmedir task.
- run rake euroads - will run only the euroads task.
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