Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my custom rake task in lib/tasks not discovered in Rails 3?

Build-in rake tasks work fine, but my new custom one, in Project/lib/tasks/payments.rb doesn't get loaded:

namespace :payments  do   desc "Tally payments at the end of the month"   task :compute => :environment do     BillingPeriod.compute_new_period   end end  $ rake payments:compute (in /Users/rob/Code/Apps/skyfarm) rake aborted! Don't know how to build task 'payments:compute' 

It works fine if I load the file application.rb:

require 'lib/tasks/payments.rb' 

...but it breaks other things:

$ rails s ./lib/tasks/payments.rb:1: undefined method `namespace' for main:Object (NoMethodError) 
like image 263
zambezi Avatar asked Apr 16 '11 21:04

zambezi


People also ask

Where to write rake task in Rails?

We can also write our custom Rake tasks in Rails environment by creating files with . rake extension in ./lib/tasks. It's a common practice after cloning a repository for the first time, to run ./bin/setup, in order to automatically fetch all the libraries, create db, seed data etc.

Where to put rake tasks?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.

What is a rake task?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.


1 Answers

Change the file extension from .rb to .rake.

like image 65
Michelle Tilley Avatar answered Oct 31 '22 11:10

Michelle Tilley