1

So, I have actually two issues, but I bet they are connected to each other.

I have to call a rake task on a legacy app (also the task is legacy) but run into "Don't know how to build task 'environment'" and "uninitilized constant" errors. We're working in Rails 5.2 with ruby 2.5.1.

The rake file is in lib/tasks/migrations/models.build_stuff.rb

It looks like this:

include ActionView::Helpers

namespace :migrations do
  namespace :build_stuff do
    task build_first_things: :environment do
      puts 'Starting building first things'
      FirstThings.each { |thing| thing.build! }
      puts 'Done!'
    end

    task build_second_things: :environment do
      puts 'Starting building second things'
      SecondThings.each { |thing| thing.build! }
      puts 'Done!'
    end
  end
end

So, I call the first task with:

rake -f ./lib/tasks/migrations/models/build_first_things.rake migrations:build_stuff:build_first_things

Here I get the following error:

rake aborted!
NameError: uninitialized constant ActionView

I know, in my example there is not even an ActionView helper used, in the real file, there is.

If, just for testing reasons, I delete the line with include ActionView::Helpers another error is raised:

rake aborted!
Don't know how to build task 'environment' (See the list of available tasks with `rake --tasks`)

I don't want to call a task 'environment', just need to load the app and access the database.

Does anyone know, why either (or) both things happen?

Thank you very very much :)

*** Solution ***

It worked when calling bin/railsor bundle exec rakeinstead of simply rake.

  • 2
    Try `bundle exec rake migrations:build_stuff:build_tax_certificates` – Sebastián Palma Jan 06 '21 at 16:11
  • Without using `bundle exec rake` or `bin/rake`, your Rails environment isn't automatically loaded. You could manually load it by requiring `config/environment.rb` in your Rake file, but that would be pretty unusual. – Unixmonkey Jan 06 '21 at 16:26
  • @Unixmokey thanks, both worked better! I however, now get another error: `rake aborted! LoadError: Unable to autoload constant FIRST_THINGS, expected /app/app/models/first_things.rb to define it` So, somehow, it looks at "app/app" instead of simply "app"... – Kevin Liebholz Jan 07 '21 at 10:14
  • Thanks guys, I got it. The last error was due to an error in the code. Correcting that and using your commands, it worked :) – Kevin Liebholz Jan 07 '21 at 11:57

0 Answers0