Skip to content

[Rails] Fix: undefined method `table_name' for ActiveRecord::SchemaMigration:Class

Published: at 10:52 AM (1 min read)

Problem

I recently upgraded Rails from 7.0.8.7 to 7.1.5.2.

Then, while running RSpec, I encountered the following error:

An error occurred in a `before(:suite)` hook.
Failure/Error: DatabaseCleaner.clean_with(:deletion)

NoMethodError:
  undefined method `table_name' for ActiveRecord::SchemaMigration:Class

            ::ActiveRecord::SchemaMigration.table_name
                                           ^^^^^^^^^^^

# ./spec/rails_helper.rb:64:in `block (2 levels) in <top (required)>'

It seems to be caused by the following code not working in Rails 7.1.

# DatabaseCleaner
config.before(:suite) do
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:deletion)
end

Solution

We can upgrade database_cleaner to version 2.1.0, which has already resolved compatibility issues with Rails 7.1 in this PR(Adds rails-7-1 support).

# Gemfile

gem 'database_cleaner', '~> 2.1.0'

Reference


Previous Post
[LeetCode] 46. Permutations
Next Post
[LeetCode] 44. Wildcard Matching