NoMethodError:升级到rake 11后的未定义方法`last_comment'

Kri*_*ris 127 rake ruby-on-rails ruby-on-rails-3

rake我运行任何任务时,我得到:

NoMethodError:未定义的方法`last_comment'for

之后bundle update推出了新版本的rake版本11.0.1.

$ grep rake Gemfile.lock
       rake
       rake (>= 0.8.7)
     rake (11.0.1)
       rake
$ bundle update
$ bundle exec rake db:drop # any rake task
Run Code Online (Sandbox Code Playgroud)

NoMethodError:#Rake :: Application:0x007ff0cf37be38>未定义的方法`last_comment'

版本

  • Rails 3.2.11
  • 耙子11.0.1

Kri*_*ris 152

Rake 11.0.1 删除了该last_comment方法Rails 2.3rspec-core(<3.4.4)使用.因此,如果/如果发布了补丁,我们需要将rake引脚到Gemfile中的旧版本:

gem 'rake', '< 11.0'
Run Code Online (Sandbox Code Playgroud)

然后:

$ bundle update
$ grep rake Gemfile.lock 
      rake
      rake (>= 0.8.7)
    rake (10.5.0)
      rake
  rake (< 11.0)
Run Code Online (Sandbox Code Playgroud)

我们现在使用rake 10.5.0仍然有last_comment方法,我们的rake任务将再次工作.

更新:现在已经在rspec中修复了,所以唯一需要的是更新rspec.

  • 谢谢你,2017年仍然保存我们的培根. (6认同)
  • 您还需要什么版本来更新rspec? (2认同)
  • @luke rspec 3.5或更高版本. (2认同)

equ*_*nt8 72

在Rails快速修复中可以编辑./Rakefile(在你的app文件夹中)

并在调用之前添加这些行Rails.application.load_tasks:

module TempFixForRakeLastComment
  def last_comment
    last_description
  end 
end
Rake::Application.send :include, TempFixForRakeLastComment
Run Code Online (Sandbox Code Playgroud)

整个Rakefile可能看起来像

  require File.expand_path('../config/application', __FILE__)
  require 'rake'
  require 'resque/tasks'

+ # temp fix for NoMethodError: undefined method `last_comment'
+ # remove when fixed in Rake 11.x
+ module TempFixForRakeLastComment
+   def last_comment
+     last_description
+   end 
+ end
+ Rake::Application.send :include, TempFixForRakeLastComment
+ ### end of temfix
+ 
  task "resque:preload" => :environment

  Rails.application.load_tasks
Run Code Online (Sandbox Code Playgroud)

  • 很好,有我的5分钟成名:D (10认同)
  • 升级到rake 12.0.0后,我开始看到这个错误.@ equivalent8的临时修复对我有用. (3认同)
  • 它似乎删除了'last_comment` [已恢复](https://github.com/ruby/rake/commit/3948349a82a2522f1d8293093164ef395bee542d),现在将在rake 12.0中删除. (2认同)

Gal*_*cha 27

更新到最新的Rspec宝石做的工作:

bundle update rspec-rails

  • 在StackOverflow上找到我自己的答案 - **3年**自从我写完之后 - 仍然像魔术一样工作:) (7认同)
  • 这不是"总是"一个好的解决方案,这可能会安装一个不兼容的rspec -rails版本,最好总是指定要使用的版本. (3认同)

小智 21

只需升级宝石 rspec-rails

现在: gem 'rspec-rails', '~> 3.5', '>= 3.5.2'

拥抱!

  • `gem 'rspec-rails', '~&gt; 3.6'` 救了我的命,非常感谢!我以为我的代码中有类似last_comment 的东西!哈哈 (2认同)

yek*_*kta 20

这是rake中已经解决的问题.

@ equivalent8的答案是一个猴子补丁,应该避免.

正如@Kris指出的那样,这是一个孤立的问题rake 11.0.1.由于@Kris已经发布了他的答案,因此可以使用Rake的新版本,理想情况下,您可以与时俱进,而不是固定在旧版本的rake上.相信我,我一直在那里,如果你可以帮助它,这不是一个好主意.这也不是Rails 2.3或任何版本的rails的问题.

任何耙子< v11.0.1> v11.0.1 and < v12将工作,但这仍然是一个解决方案,也应该避免; 理想情况下,你将能够与时俱进.

由于last_comment被弃用,因此应该升级依赖项本身.在我的情况下,rspec-core它偶然只在v3.4.4中解决了这个问题.

修复

将依赖项升级到不调用last_comment但调用的版本last_description.它可能rspec并升级rspec-core到3.4.4或更高将解决它. rspec-core<3.4.4电话last_comment.

如果您的依赖项没有不调用的版本,请last_description成为一个好公民并提交PR来修复它:)