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.

4366 questions
70
votes
1 answer

Does rake db:schema:dump recreate schema.rb from migrations or the database itself?

Does rake db:schema:dump recreate schema.rb from migrations or the database itself?
pingu
  • 8,719
  • 12
  • 50
  • 84
69
votes
2 answers

Using the seed value from rake in unit and functional tests

When running unit and functional tests using rake, on a rails application, I notice that there is a seed value which is specified on the command line: --seed x $ rake test (in /code/blah) Loaded suite…
VirtualStaticVoid
  • 1,656
  • 3
  • 15
  • 21
67
votes
2 answers

How to list all Rails 3 rake tasks? rake -T seems incomplete

Possible Duplicate: Why is rake db:migrate:reset not listed in rake -T? Inside my Rails (3.1.3) application, I can run rake db:test:prepare. But when I run rake -T [db], that task (and many others I can use) does not show up. How do I get a…
Felix Rabe
  • 4,206
  • 4
  • 25
  • 34
62
votes
4 answers

Including rake tasks in gems

1) Is there a 'best' place for rake tasks inside of gems? I've seen them in /tasks, /lib/tasks, and I've seen them written as *.rb and *.rake -- not sure which (if any) is 'correct' 2) How do I make them available to the app once the gem is…
jsharpe
  • 2,546
  • 3
  • 26
  • 42
61
votes
1 answer

How to get PID of current rake task?

I'm putting in a reaper line into a rake task to kill some additionally spawned ruby tasks as they somehow creep up on occasion. system "ps aux | grep 'namespace:taskname' | grep ruby | grep -v grep | awk '{print $2}' | xargs kill -9; echo 'Reaped…
ylluminate
  • 12,102
  • 17
  • 78
  • 152
61
votes
4 answers

How do I run Rake tasks within a Ruby script?

I have a Rakefile with a Rake task that I would normally call from the command line: rake blog:post Title I'd like to write a Ruby script that calls that Rake task multiple times, but the only solution I see is shelling out using `` (backticks) or…
Mando Escamilla
  • 1,560
  • 1
  • 10
  • 17
59
votes
1 answer

What is the difference between Rails.cache.clear and rake tmp:cache:clear?

Are the two commands equivalent? If not, what's the difference?
Crashalot
  • 33,605
  • 61
  • 269
  • 439
58
votes
6 answers

Running Rake tasks in Rspec Tests

I am building up an integration test suite and there is one bit of logic that I need to have a clean database for. How can I run the db:test:purge task inside of one of my tests? I am using: ruby 1.9.2, rails 3.0.9, rspec 2.6
xentek
  • 2,555
  • 2
  • 20
  • 12
57
votes
4 answers

Create seed file from data already in the database

I'm using Rails 3.0.3 and have data for my "categories" table already in the database, but want to create a seed file from it. Is there any rake task that will generate the seeds.rb format for me from this table?
swrobel
  • 4,053
  • 2
  • 33
  • 42
56
votes
2 answers

Model.reset_column_information does not reload columns in rails migration

I'm using Rails 3.2 and have a migration that contains the code: add_column :users, :gift_aid, :integer, :default => 2 # reset columns User.reset_column_information ... code here to load legacy data from sqlite3 database ... # now create a user…
Iain
  • 942
  • 1
  • 8
  • 10
55
votes
4 answers

When I run the rake:db migrate command I get an error "Uninitialized constant CreateArticles"

I created a model ruby script/generate model Article (simple enuff) Here is the migration file create_articles.rb: def self.up create_table :articles do |t| t.column :user_id, :integer t.column :title, :string t.column :synopsis,…
featureBlend
  • 667
  • 1
  • 8
  • 10
54
votes
4 answers

Is it possible to make an interactive Rake task?

I want to run a Rake task that asks the user for input. I know that I can supply input on the command line, but I want to ask the user if they are sure they want to proceed with a particular action in case they mistyped one of the values supplied…
RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193
54
votes
5 answers

Simple rails rake task refuse to run with error "Don't know how to build task", why?

I have this simple rake task which refuses to run. I just don't see why it looks correct. Who can pinpoint me to the probably very simple mistake I made? Thank you! /lib/tasks/reindex.rb: namespace :db do desc "Tire reindex profiles" task…
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
53
votes
4 answers

How do I find the source file for a rake task?

I know you can view all possible rake tasks by typing rake -T But I need to know what exactly a task does. From the output, how can I find a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump…
Tilendor
  • 48,165
  • 17
  • 52
  • 58
53
votes
2 answers

How to explicitly fail a task in ruby rake?

Let's say I have a rakefile like this: file 'file1' => some_dependencies do sh 'external tool I do not have control over, which sometimes fail to create the file' ??? end task :default => 'file1' do puts "everything's OK" end Now if I put…
dahpgjgamgan
  • 2,977
  • 4
  • 25
  • 26