<この記事について>
自作のアプリ製作中にカラムを追加する流れで「rails:db:migrate」を実行すると「Directly inheriting from ActiveRecord::Migration is not supported.」と表示されエラーに。
初歩的なミスでしたが備忘録として投稿!
[環境]
・Ruby 2.6.5,
・Rails 6.0.0
・macOS
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
調べてみると、原因はマイグレーションファイルがrailsのバージョンに対応していないことが原因のようでした。。
対象のマイグレーションファイルの1行目末尾にrailsのバージョンを追加。
class AddAttachmentImageToPosts < ActiveRecord::Migration[6.0] #←[6.0]を追加
def self.up
change_table :posts do |t|
t.attachment :image
end
end
def self.down
remove_attachment :posts, :image
end
end
その後、再度以下を実行すると解決しました。
$ rails db:migarate
<最後に>
実装してエラーが出る、思うような結果が得られないことは多々あると思います。
そんな時は冷静な気持ちでエラー文と丁寧に向き合うようにしましょう!