Skip to content

Instantly share code, notes, and snippets.

@olistik
Created February 18, 2014 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olistik/9068351 to your computer and use it in GitHub Desktop.
Save olistik/9068351 to your computer and use it in GitHub Desktop.
Rails.logger is nil when invoking rake tasks [Ruby: 2.1.0p0, Rails: 4.0.2]
task :foo do
if Rails.logger.nil?
puts "WAT"
end
end
ಠ_ಠ rails -v
Rails 4.0.2
ಠ_ಠ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]
ಠ_ಠ rails new mytest
....
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
ಠ_ಠ cd mytest/
ಠ_ಠ cat lib/tasks/mytask.rake
task :foo do
if Rails.logger.nil?
puts "WAT"
end
end
ಠ_ಠ rake foo
WAT
ಠ_ಠ
@olistik
Copy link
Author

olistik commented Feb 18, 2014

This ML reply helped me understand that I was forgetting to require the :environment task.

task :foo => [:environment] do
  if Rails.logger.nil?
    puts "WAT"
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment