Questions tagged [rake]

Ruby build utility similar to make with build commands defined in pure Ruby.

Rake uses Rakefiles (cf. Makefiles) defined in pure Ruby, the build utility created by the late Jim Weirich. Rule patterns to synthesize implicit tasks are supported and developers can easily specify pre- and post-task dependency chains. Rake includes a library of pre-packaged tasks to make creating Rakefiles easier.

Rake uses Ruby's anonymous function blocks to define various tasks, allowing the use of Ruby syntax. It has a library of common tasks: for example, functions to do common file-manipulation tasks and a library to remove compiled files (the "clean" task). Like Make, Rake can also synthesize tasks based on patterns: for example, automatically building a file compilation task based on filename patterns. Rake is now part of the standard library from Ruby version 1.9 onward.

Sample Rake file

    namespace :pick do
      desc "Pick a random user as the winner"
      task :winner => :environment do
        puts "Winner: #{pick(User).name}"

      desc "Pick a random product as the prize"
      task :prize => :environment do
        puts "Prize: #{pick(Product).name}"

      desc "Pick a random prize and winner"
      task :all => [:prize, :winner]

      def pick(model_class)
        model_class.find(:first, :order => 'RAND()')

Useful links

Implementation specific tags

You can specify your question by adding the implementation you used as a tag.

4366 questions
21 answers

How to pass command line arguments to a rake task

I have a rake task that needs to insert a value into multiple databases. I'd like to pass this value into the rake task from the command line, or from another rake task. How can I do this?
  • 48,165
  • 17
  • 52
  • 58
6 answers

Difference between rake db:migrate db:reset and db:schema:load

The difference between rake db:migrate and rake db:reset is pretty clear to me. The thing which I don't understand is how rake db:schema:load is different from the former two. Just to be sure that I am on the same page: rake db:migrate - Runs the…
Gaurav Agarwal
  • 14,664
  • 4
  • 29
  • 41
22 answers

Purge or recreate a Ruby on Rails database

I have a dev Ruby on Rails database full of data. I want to delete everything and rebuild the database. I'm thinking of using something like: rake db:recreate Is this possible?
  • 108,152
  • 195
  • 629
  • 1,012
7 answers

How to run Rake tasks from within Rake tasks?

I have a Rakefile that compiles the project in two ways, according to the global variable $build_type, which can be :debug or :release (the results go in separate directories): task :build => [:some_other_tasks] do end I wish to create a task that…
7 answers

What does bundle exec rake mean?

What does bundle exec rake db:migrate mean? Or just bundle exec rake in general? I understand that bundle takes care of maintaining things in the Gemfile. I know what the word "exec" means. I understand that rake maintains all the…
  • 24,245
  • 28
  • 105
  • 147
21 answers

A cron job for rails: best practices?

What's the best way to run scheduled tasks in a Rails environment? Script/runner? Rake? I would like to run the task every few minutes.
  • 18,324
  • 12
  • 36
  • 40
7 answers

How do I return early from a rake task?

I have a rake task where I do some checks at the beginning, if one of the checks fails I would like to return early from the rake task, I don't want to execute any of the remaining code. I thought the solution would be to place a return where I…
  • 5,025
  • 8
  • 34
  • 43
6 answers

Do rails rake tasks provide access to ActiveRecord models?

I am trying to create a custom rake task, but it seems I dont have access to my models. I thought this was something implicitly included with rails task. I have the following code in lib/tasks/test.rake: namespace :test do task :new_task do …
  • 7,975
  • 4
  • 27
  • 30
6 answers

How to rollback just one step using rake db:migrate

After adding migration files in the db/migrate folder and running rake db:migrate, I want get back to the previous step, I think using VERSION=n is the right way to do that, but I don't know the correct value of n to use. Is there any command to…
  • 21,334
  • 49
  • 130
  • 191
5 answers

How to pass arguments into a Rake task with environment in Rails?

I am able to pass in arguments as follows: desc "Testing args" task: :hello, :user, :message do |t, args| args.with_defaults(:message => "Thanks for logging on") puts "Hello #{args[:user]}. #{:message}" end I am also able to load the current…
  • 8,102
  • 5
  • 30
  • 32
19 answers

Ruby on Rails and Rake problems: uninitialized constant Rake::DSL

I'm having a really frustrating issue: Rake is being dumb. Here's how the problem comes about: $ rails new test_app $ rails generate scaffold new_scaffold field1:string field2:text Both of those work just fine, but then when I do this, $ rake…
  • 5,842
  • 5
  • 32
  • 35
14 answers

How to run a single test from a Rails test suite?

How can I run a single test from a Rails test suite? rake test ANYTHING seems to not help.
  • 6,786
  • 7
  • 47
  • 71
7 answers

Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

The latest changesets to Ruby 1.9.2 no longer make the current directory . part of your LOAD_PATH. I have a non-trivial number of Rakefiles that assume that . is part of the LOAD_PATH, so this broke them (they reported "no such file to load" for all…
John Feminella
  • 303,634
  • 46
  • 339
  • 357
3 answers

What's the 'environment' task in Rake?

According to "Custom Rake Tasks": desc "Pick a random user as the winner" task :winner => :environment do puts "Winner: #{pick(User).name}" end As far as I know, the :winner => :environment means "do environment before winner". But what's…
Lai Yu-Hsuan
  • 27,509
  • 28
  • 97
  • 164
5 answers

NoMethodError: undefined method `last_comment' after upgrading to rake 11

When running any rake task I get: NoMethodError: undefined method `last_comment' for This was after bundle update which pulled in the new version of rake, version 11.0.1. $ grep rake Gemfile.lock rake rake (>= 0.8.7) rake…
  • 19,188
  • 9
  • 91
  • 111
2 3
99 100