[ruby-on-rails] Rails how to run rake task

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

This question is related to ruby-on-rails ruby rake

The answer is


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

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

or

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

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.


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
    

Have you tried rake reklamer:iqmedier ?

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


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

Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to rake

Bundler::GemNotFound: Could not find rake-10.3.2 in any of the sources Difference between rake db:migrate db:reset and db:schema:load Rails and PostgreSQL: Role postgres does not exist What does bundle exec rake mean? "Could not find bundler" error Rails how to run rake task How to rollback just one step using rake db:migrate Purge or recreate a Ruby on Rails database Rails DB Migration - How To Drop a Table? how to do "press enter to exit" in batch