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}"
      end

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

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

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

Useful links

Implementation specific tags

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

4310 questions
1139
votes
19 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?
Tilendor
  • 45,619
  • 15
  • 48
  • 58
660
votes
5 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 in my head. The thing which I don't understand is how rake db:schema:load different from the former two. Just to be sure that I am on the same page: rake db:migrate - Runs the…
Gaurav Agarwal
  • 13,952
  • 4
  • 27
  • 39
609
votes
21 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?
AnApprentice
  • 97,752
  • 174
  • 583
  • 971
532
votes
22 answers

Rails DB Migration - How To Drop a Table?

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table? I've already run migrations, so the table is in my database. I figure rails generate migration should be able to handle this, but…
Jason Whitehorn
  • 12,933
  • 9
  • 51
  • 68
424
votes
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…
Arry
368
votes
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…
JnBrymn
  • 21,527
  • 26
  • 95
  • 140
302
votes
20 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.
jes5199
  • 16,375
  • 11
  • 35
  • 40
241
votes
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 …
gmoniey
  • 7,675
  • 4
  • 25
  • 29
238
votes
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…
Janak
  • 4,705
  • 8
  • 31
  • 41
217
votes
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…
Will
  • 7,732
  • 5
  • 26
  • 31
206
votes
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…
mko
  • 19,044
  • 43
  • 119
  • 183
190
votes
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…
HRÓÐÓLFR
  • 5,494
  • 5
  • 30
  • 32
154
votes
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
  • 281,997
  • 42
  • 326
  • 347
137
votes
12 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.
artemave
  • 6,267
  • 5
  • 44
  • 68
133
votes
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…
Kris
  • 16,882
  • 6
  • 79
  • 99
1
2 3
99 100