【rails5.2.0】rails db:migrateをする際に発生したDirectly inheriting from ActiveRecord::Migration is not supported.の対処法について

f:id:daikiyano:20181105234416p:plain

今回、【rails5.2.0】rails db:migrateをする際に生じたエラー「Directly inheriting from ActiveRecord::Migration is not supported.」の対処法についてご紹介します。

経緯

github.com

今回、cdeditorというリッチテキスト(WYSIWYG)エディタをrails5に導入するため、以下のgemを追加しbundle installをコマンド上で実行。

gem 'ckeditor'
gem 'rmagick'
gem 'mini_magick'

bundle install後、以下のmigrationファイルが生成されます。

db/migrate/20181105113219_create_ckeditor_assets.rb
lass CreateCkeditorAssets < ActiveRecord::Migration
def self.up
create_table :ckeditor_assets do |t|
t.string  :data_file_name, null: false
t.string  :data_content_type
t.integer :data_file_size
t.string  :type, limit: 30
# Uncomment it to save images dimensions, if your need it
t.integer :width
t.integer :height
t.timestamps null: false
end
add_index :ckeditor_assets, :type
end
def self.down
drop_table :ckeditor_assets
end
end

migrationファイル生成後、rails db migrateを実行すると以下のようなエラーが発生

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:

そもそもリッチテキストエディタ、WYSIWYGエディタとは?

engineer.blog.lancers.jp

リッチテキスト(WYSIWYG)エディタとは,HTMLの知識がなくても見出しや文字の斜体や強調、URLの貼り付けなどの直感的に操作できるエディタです。

つまり、以下の画像のようなエディタですね。
ブロガーさんは物凄くイメージが湧きやすいと思います。
私自身も、リッチテキスト(WYSIWYG)エディタを使いながら記事の編集を行なっています。

f:id:daikiyano:20181105234416p:plain

今回のエラーの解決策

以下サイトを参照したところ、migrationファイルがrails5に対応していなかったため生じたエラーであることが判明。

blog.mrym.tv

class CreateCkeditorAssets < ActiveRecord::Migration[5.2]

よって先ほどのmigrationファイルの横に[5.2]を追加し、rails db:migrateをすれば、
無事にマイグレーションが通ります!

備忘録記事になってしまいましたが、許してください。

>文系エンジニア大学生の技術ブログ

文系エンジニア大学生の技術ブログ

社会が多様化していく中、大学生の学生生活も多様であるべきと考えています。主にエンジニア向けにITやプログラミングなどの技術系と大学生向けに休学、留学、海外生活、トビタテ留学、長期インターンに関する記事を書いています。