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

Ruby on-RailsRuby on-Rails-3Rake

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

Update to the latest Rspec gem does the work:

bundle update rspec-rails

Solution 4 - Ruby on-Rails

Just upgrade the gem rspec-rails

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

hugs!

Solution 5 - Ruby on-Rails

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 :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrisView Question on Stackoverflow
Solution 1 - Ruby on-RailsKrisView Answer on Stackoverflow
Solution 2 - Ruby on-Railsequivalent8View Answer on Stackoverflow
Solution 3 - Ruby on-RailsGal BrachaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsEderCostaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsyektaView Answer on Stackoverflow