Ruby on Rails执行rake db:migrate报NoMethodError错误求助
undefined method '[]' for ActiveRecord::Migration Error in Rails 4.2.5 Hey there! Let's sort out this migration error you're facing while setting up OmniAuth for Facebook and Google auth.
The Root Cause
Looking at your error trace and migration file, the problem is straightforward: you're using Rails 4.2.5, but your migration uses a syntax introduced in Rails 5.0. The line class UpdateUsers < ActiveRecord::Migration[5.0] uses the versioned migration syntax ([5.0]) which isn't supported in Rails 4.x. The ActiveRecord::Migration class in Rails 4 doesn't have the [] method, hence the NoMethodError you're seeing.
The Fix
Update your migration file to use the Rails 4.x-compatible migration syntax by removing the version tag in square brackets:
class UpdateUsers < ActiveRecord::Migration def change add_column(:users, :provider, :string, limit: 50, null: false, default: '') add_column(:users, :uid, :string, limit: 500, null: false, default: '') end end
Next Steps
- If you already tried running the migration and it failed, you don't need to rollback (since it didn't execute successfully), but if you need to reset the migration state for any reason, you can run:
rake db:rollback - Re-run your migration command:
rake db:migrate
Quick Note
Versioned migrations (ActiveRecord::Migration[X.X]) are a Rails 5+ feature. Since you're on Rails 4.2.5, sticking to the unversioned ActiveRecord::Migration base class will resolve this issue. Once you upgrade to Rails 5 or later, you can start using the versioned syntax again for better compatibility across Rails versions.
内容的提问来源于stack exchange,提问作者Shreya Choubey

