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)
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.
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.
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.
Change the file extension from .rb
to .rake
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With