不建议全局访问 Rake DSL 方法

我正在阅读 Ruby on Rails 3教程,并在命令行中输入了以下内容:

rake db:migrate

产生以下警告。

WARNING: Global access to Rake DSL methods is deprecated.  Please Include
...  Rake::DSL into classes and modules which use the Rake DSL methods.


WARNING: DSL method DemoApp::Application#task called at /Users/imac/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.7/lib/rails/application.rb:215:in `initialize_tasks'

我不知道该怎么办,也不知道如何应对。我不知道雷克还有什么指挥官。

我怎样才能解决这个问题?

22734 次浏览

I found this in Stack Overflow question Ruby on Rails and Rake problems: uninitialized constant Rake::DSL. It refers to a @DHH tweet.

Put the following in your Gemfile

gem "rake", "0.8.7"

You may see something like

rake aborted!
You have already activated Rake 0.9.1 ...

I still had a copy of Rake 0.9.1 in my directory so I deleted it.

You can "delete" Rake 0.9.1 by running the following command:

gem uninstall rake -v=0.9.1

If you have multiple versions of the gem installed, you'll be prompted to pick a version.

After 0.9.1 was cleaned out, I ran

bundle update rake

and was finally able to create my database files. I was using rake db:create, but it should work for rake db:migrate as well.

I hope it helps.

Adding include Rake::DSL to the Rakefile before the applications load_tasks were called also worked for me.

So in the above user's case before the DemoApp::Application.load_tasks in the Rakefile.

I was having the same problem on Windows with the installer. Ruby 1.9.2 and Rails 3.0.9. Here is what I did:

bundle update rake
bundle show rake

After doing that I was running rake 0.9.2.

Then I updated the Rakefile in application root folder as follows:

require File.expand_path('../config/application', __FILE__)
require 'rake'
# If you named your application something other than SampleApp, change that below
module ::SampleApp
class Application
include Rake::DSL
end
end


module ::RakeFileUtils
extend Rake::FileUtilsExt
end


SampleApp::Application.load_tasks

As noted in the comment, make sure the name of your app is correct in the two appropriate lines above.

If you are seeing this on later versions of Rails (like 3.+) you may also want to verify that your environment is clean by using RVM http://beginrescueend.com/ and creating a specific ruby & gemset for your projects.

Use an .rvmrc file on a per-project basis, this will guarantee you aren't getting older system gems into your projects. Which has bitten me before.

This prevents having to monkey around with generated Rakefiles & such.

bundle exec rake db:migrate will solve your ruby version issues