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)

Change the file extension from .rb to .rake.


In this specific case, not having a .rake extension caused the error. However, I had the same issue with a Rails 4.2 app today, and it was because I did not have a desc for my rake task, so make sure if you're writing your own task (i.e. not generating one) that you add a desc.

For more information: http://guides.rubyonrails.org/command_line.html#custom-rake-tasks