54

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 :reindex => :environment do
    system "cd #{Rails.root} && rake environment tire:import CLASS='Profile' FORCE=true"
  end

end

The error:

rake db:reindex
rake aborted!
Don't know how to build task 'db:reindex'
tokyovariable
  • 1,656
  • 15
  • 23
Rubytastic
  • 15,001
  • 18
  • 87
  • 175

5 Answers5

105

Rename your file to reindex.rake and it should work.

Related: How to build task 'db:populate'

Community
  • 1
  • 1
cjc343
  • 3,735
  • 1
  • 29
  • 34
  • thx, this was indeed the problem, and easy missed when just looking at the file contents wondering why things won't work! – Rubytastic Oct 15 '12 at 21:15
  • 4
    The significant thing, which I missed at first, is that the file extension needs to be `.rake` as opposed to `.rb`. – Jason Swett Mar 21 '16 at 14:46
  • 3
    Second time I arrived here. Tried to upvote, already had. I should have learnt my lesson. Damnit. – sandre89 Feb 11 '19 at 14:00
16

You can also get this error if you forget to put the namespace before your task name. (i.e. :reindex instead of db:reindex)

esc_rtn
  • 936
  • 9
  • 7
13

The file extension for rake tasks must be '.rake'.

If you named your file as '.rb', then rake will not find it, and you will question your own sanity for several minutes before ending up here.

David Hempy
  • 5,373
  • 2
  • 40
  • 68
  • 2
    Isn't this a duplicate of the [accepted answer](https://stackoverflow.com/a/12903234/354577)? – ChrisGPT was on strike Apr 16 '19 at 17:55
  • 2
    Largely, yes. But I added emphasis on a common incorrect approach (e.g. "Did you name yours .rb?") to help folks like myself who glossed over the first answer with, "Yes, my file has a ruby-looking extension...that's not why Ruby hates me.") – David Hempy Nov 12 '19 at 20:48
2

Don't forget to check that you call the name of the task and not the file name. The best thing is that they be named the same.

Morgan
  • 37
  • 6
2

This error happen to me is because the namespace name got underscore

As is: deploy_app  (not work)
To be: deployapp   (working)
super1ha1
  • 629
  • 1
  • 10
  • 17