【问题标题】:NoMethodError: undefined method `last_comment' after upgrading to rake 11NoMethodError:升级到 rake 11 后未定义方法“last_comment”
【发布时间】:2023-04-03 23:25:01
【问题描述】:

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

NoMethodError: 未定义方法“last_comment”

这是在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

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

版本

  • Rails 3.2.11
  • 耙 11.0.1

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rake


    【解决方案1】:
    Rails 2.3 rspec-core (Rake 11.0.1 removes last_comment 方法。因此,直到/如果补丁发布,我们需要将 rake 固定到 Gemfile 中的旧版本:
    gem 'rake', '< 11.0'
    

    然后:

    $ bundle update
    $ grep rake Gemfile.lock 
          rake
          rake (>= 0.8.7)
        rake (10.5.0)
          rake
      rake (< 11.0)
    

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

    更新:这已在 rspec 中得到修复,因此唯一需要做的就是更新 rspec。

    【讨论】:

    • 我必须在这样做之后使用bundle exec,如何在没有捆绑执行的情况下仍然使用rake??
    • 你能澄清你的“Rails 2.3”使用评论吗?我在 Rails 4 上,rake db:create:all 抛出了这个异常。
    • @yekta Rails 2.3 期望 Rake 有一个名为 last_comment 的方法,我不记得在代码库中的哪个位置。我不知道 Rails 4 是否也调用last_comment。但是您可以检查 rails 代码库,切换到 Rails 4 分支并在 Rakefile*.task 中使用 grep 查找“last_comment”。
    • 感谢您的支持,2017 年我们的培根仍在保存。
    • @luke rspec 3.5 或更高版本。
    【解决方案2】:

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

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

    module TempFixForRakeLastComment
      def last_comment
        last_description
      end 
    end
    Rake::Application.send :include, TempFixForRakeLastComment
    

    所以整个 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
    

    【讨论】:

    • 谢谢。从 Rake 11.1.0 开始不再需要
    • 很好,我的 5 分钟成名了:D
    • 看起来last_comment got reverted 被删除了,现在将在 rake 12.0 中被删除。
    • 升级到 rake 12.0.0 后,我开始看到这个错误。 @equivalent8 的临时修复对我有用。
    • 升级到 rspec-core-3.5.4 为我解决了这个问题,我能够在这个答案中删除猴子补丁。
    【解决方案3】:

    更新到最新的Rspec gem 即可:

    bundle update rspec-rails

    【讨论】:

    • 在 StackOverflow 上找到我自己的答案 - 3 年 自从我写了它 - 仍然像魔术一样工作 :)
    • 这不是“总是”好的解决方案,这可能会安装不兼容的 rspec -rails 版本,最好始终指定要使用的版本。
    【解决方案4】:

    只需升级 gem rspec-rails

    现在:gem 'rspec-rails', '~&gt; 3.5', '&gt;= 3.5.2'

    拥抱!

    【讨论】:

    • gem 'rspec-rails', '~&gt; 3.6' 救了我的命,非常感谢!!而且我认为我的代码中有类似 last_comment 的内容!哈哈
    【解决方案5】:

    这是一个issue in rake,已被处理。

    @equivalent8 的回答是猴子补丁,应该避免。

    正如@Kris 指出的那样,这是一个与rake 11.0.1 无关的问题。由于@Kris 发布了他的答案,因此有新版本的 Rake 可用,理想情况下,您将能够与时俱进,而不会被固定在旧版本的 rake 上。相信我,我去过那里,如果你能帮忙,这不是一个好主意。这也不是 Rails 2.3 或任何版本的 rails 的问题。

    任何 Rake &lt; v11.0.1&gt; v11.0.1 and &lt; v12 都可以,但这仍然是一种解决方法,也应该避免;理想情况下,您将能够与时俱进。

    由于 last_comment 已被弃用,因此应升级依赖项本身。在我的情况下,它是rspec-core,它只是在v3.4.4 中修复了这个问题。

    修复

    将您的依赖项升级到不调用last_comment 而是调用last_description 的版本。它可能是 rspec 并将 rspec-core 升级到 3.4.4 或更高版本将修复它。 rspec-corelast_comment.

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-08
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多