Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method `needs_migration?' for ActiveRecord::Migrator:Class

I'm getting the below error message, not sure how to resolve it. Can anyone help please?

NoMethodError: undefined method `needs_migration?' for ActiveRecord::Migrator:Class

Here is the config.ru code:

require './config/environment'

if ActiveRecord::Migrator.needs_migration?
  raise 'Migrations are pending. Run `rake db:migrate` to resolve the issue.'
end

use Rack::MethodOverride

use UsersController
use ArtworkController
run ApplicationController
like image 853
liquidsword Avatar asked Jul 16 '26 08:07

liquidsword


2 Answers

change your code to

if ActiveRecord::Base.connection.migration_context.needs_migration?
  raise 'Migrations are pending. Run `rake db:migrate` to resolve the issue.'
end
like image 100
Ravi Mariya Avatar answered Jul 18 '26 23:07

Ravi Mariya


Fast forward to Rails 7.2, when they finally removed the long-deprecated method:

ActiveRecord::Base.connection.migration_context.needs_migration?

As of Rails 7.2 (or earlier?) the way to test for missing migration in code looks like:

ActiveRecord::Migration.check_all_pending!

That method doesn't return a boolean, but raises an error if migrations are pending. To boolify it you have to rescue. Something like this:

  def migrations_missing?
    ActiveRecord::Migration.check_all_pending!
    false
  rescue ActiveRecord::PendingMigrationError
    true
  end
like image 42
David Hempy Avatar answered Jul 18 '26 23:07

David Hempy