NoMethodError: undefined method `last_comment' after upgrading to rake 11

42,565

Solution 1

Rake 11.0.1 removes the last_comment method which Rails 2.3 rspec-core (< 3.4.4) uses. Therefore until/if a patch is released we need to pin rake to an older version in Gemfile:

gem 'rake', '< 11.0'

then:

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

We are now using rake 10.5.0 which still has the last_comment method and our rake tasks will work again.

UPDATE: This has now been fixed in rspec, so the only thing necessary should be updating rspec.

Solution 2

in Rails quick fix can be edit ./Rakefile (in your app folder)

and add these lines before calling Rails.application.load_tasks:

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

so entire Rakefile might look like

  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

Solution 3

Update to the latest Rspec gem does the work:

bundle update rspec-rails

Solution 4

Just upgrade the gem rspec-rails

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

hugs!

Solution 5

This is an issue in rake that has been addressed already.

The answer by @equivalent8 is a monkey patch and should be avoided.

As @Kris points out, this is an issue isolated to rake 11.0.1. Since @Kris has posted his answer there are new versions of Rake available and ideally you would be able to stay with the times and not be pinned to an old version of rake. Believe me, I've been there and its not a good idea if you can help it. Also this is not an issue with Rails 2.3 or any version of rails.

Any Rake < v11.0.1 or > v11.0.1 and < v12 will work but this is still a work around and should also be avoided; ideally you'll be able to stay with the times.

Since last_comment is being deprecated the dependency itself should be upgraded. In my case it was rspec-core which incidentally only fixed this in v3.4.4.

The Fix

Upgrade your dependency to a version which doesn't call last_comment but calls last_description instead. Its probably rspec and upgrading rspec-core to 3.4.4 or greater will fix it. rspec-core < 3.4.4 calls last_comment.

If your dependency doesn't have a version which doesn't call last_description, be a good citizen and submit a PR to fix it :)

Share:
42,565
Kris
Author by

Kris

Cynefin, Complexity, Agile, Business, Architecture, Teams

Updated on November 02, 2020

Comments

  • Kris
    Kris over 3 years

    When running any rake task I get:

    NoMethodError: undefined method `last_comment' for

    This was after bundle update which pulled in the new version of rake, version 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: undefined method `last_comment' for #< Rake::Application:0x007ff0cf37be38>

    Versions

    • Rails 3.2.11
    • Rake 11.0.1
  • svelandiag
    svelandiag about 8 years
    I have to use bundle exec after do this, how to just still use rake without the bundle exec??
  • sethcall
    sethcall about 8 years
    Thanks. No longer necessary as of Rake 11.1.0
  • equivalent8
    equivalent8 about 8 years
    nice, there goes my 5 minutes of fame :D
  • jasnow
    jasnow about 8 years
    Still getting it with Rake 11.1.1.
  • Kris
    Kris about 8 years
    It looks like the removal of last_comment got reverted and will now be removed in rake 12.0 instead.
  • equivalent8
    equivalent8 about 8 years
    hmmm, I guess by that time most of gems will fix this code dependency
  • yekta
    yekta about 8 years
    Can you clarify your "Rails 2.3" uses comment? I'm on Rails 4 and a rake db:create:all throws this exception.
  • Kris
    Kris about 8 years
    @yekta Rails 2.3 expects Rake to have a method called last_comment, where in the codebase this is I do not remember. I do not know if Rails 4 also calls last_comment. But you could checkout the rails codebase, switch to the Rails 4 branch and grep for "last_comment" in Rakefile and *.task.
  • yekta
    yekta about 8 years
    Rails 2.3 and Rails 4 do not have any call to last_comment, although there are references to it in the rails tests, they would not cause this error. #confused
  • Kris
    Kris about 8 years
    @yekta Does you stacktrace for the error tell you where it is called?
  • yekta
    yekta about 8 years
    Thanks for the good questions Kris, I just posted an answer which addresses my (the?) root issue.
  • James Tan
    James Tan over 7 years
    is there a way to solve this upgrade sanity issue? it seems to happen every time any upgrade happen...
  • blackwood
    blackwood over 7 years
    After upgrade to rake 12.0.0 I started seeing this error. @equivalent8's temp fix worked for me.
  • neontapir
    neontapir over 7 years
    Upgrading to rspec-core-3.5.4 resolved this for me and I was able to remove the monkey patch in this answer.
  • Nick Schwaderer
    Nick Schwaderer about 7 years
    Thank you for this, still saving our bacon in 2017.
  • Justin Force
    Justin Force almost 7 years
    Still a great fix if for some reason you just can't upgrade rspec or rake.
  • user1735921
    user1735921 over 6 years
    gem 'rspec-rails', '~> 3.6' saved my live, thanks a lot!! And i thought that I have something like last_comment inside my code! LOL
  • Gal Bracha
    Gal Bracha over 6 years
    Finding my own answer on StackOverflow - 3 Years Since I've wrote it - Still works like magic :)
  • Arnold Roa
    Arnold Roa over 6 years
    this is not "always" a good solution, this could install a non compatible rspec -rails version, it's better to always specify the verison to use.
  • Kris
    Kris about 6 years
    @luke rspec 3.5 or greater.