32

I have a problem when I do:

namespace :xaaron do
  task :get_roles do
    roles = Xaaron::Role.all
    puts roles
  end
  
  task :get_role, [:name] do |t, args|
    role = Xaaron::Role.find(args[:name].parameterize)
    puts role
  end
end

The first task will work fine. I can even add binding.pry and run Xaaron::Role and get information about Roles back. But the second task fails with:

NameError: uninitialized constant Xaaron::Role

I run each task in my main app because these tasks are inside an engine, using:

bin/rake xaaron:get_roles` and `bin/rake xaaron:get_role

I can run bin/rails c in the main application that uses the engine and run Xaaron::Role and get information about Roles table.

Why is the second one failing but the first one is not? Is there scoping with arguments?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
SeekingTruth
  • 1,044
  • 2
  • 15
  • 23

5 Answers5

81

I'm not sure why either works, but if this is Rails and those are Rails models, your tasks should depend on the environment:

task :get_roles => [ :environment ] do

By depending on the :environment task, it first loads Rails.

Also see: What's the 'environment' task in Rake?.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
DGM
  • 26,629
  • 7
  • 58
  • 79
  • obviously it rails and I think it loads the default environment. Be it development or production or what ever. But ill give what you say a try based on votes to your answer. – SeekingTruth Dec 11 '14 at 22:14
  • 1
    You also cannot do task `:something => [:enviroment], [:param] do |t, args| ... end` with your "example" can you clarify how I might pass in arguments? – SeekingTruth Dec 11 '14 at 22:19
  • 2
    try `:something, [:param] => [:environment]` – DGM Dec 11 '14 at 22:37
12

You can also run a Rake task as

bundle exec rake environment xaaron:get_role

This will load the Rails environment first.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Max Prokopov
  • 1,308
  • 11
  • 16
1

I kept getting uninitialized constant errors for a Rake task, even after depending on :environment and running with bundle exec.

The issue was that I was making a Rake::TestTask and, even though the Rake task had access to all constants, the test files themselves did not have access to constants.

The solution was to add this line to the top of my test file:

require_relative '../config/environment'

This is the Rake task:

require "rake/testtask"

Rake::TestTask.new(:test) do |t|
  t.libs << "test"
  t.libs << "lib"
  t.test_files = FileList["test/**/test_*.rb"]
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user3827510
  • 149
  • 1
  • 7
0

To add, as of Ruby 1.9 and above, you can use this hash syntax:

namespace :xaaron do
  desc "Rake task to get roles"
  task get_roles: :environment do
    roles = Xaaron::Role.all
    puts roles
  end
  #####

end

And then you can run the command below to run the Rake task:

rake xaaron:get_roles

or

bundle exec rake xaaron:get_roles
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
0

config.autoload_paths += %W(#{config.root}/lib)

in application.rb that work for me

DoanLe
  • 1
  • 2