#2667·paperclip

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

Author: SouravgoswamiCreated Sep 19, 2020Updated Apr 15, 2023

Deprecation notice

Paperclip is currently undergoing deprecation in favor of ActiveStorage. Maintainers of this repository will no longer be tending to new issues. We're leaving the issues page open so Paperclip users can still see & search through old issues, and continue existing discussions if they wish.

I know this is deprecated, but I am doing a course where I have to use this gem instead of ActiveStorage or Carrierwave.

The problem is, in Rails 6.0, whenever I run rails db:{drop,create,migrate} I get:

Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'
== 20200914221802 CreatePictures: migrating ===================================
-- create_table(:pictures)
   -> 0.0048s
== 20200914221802 CreatePictures: migrated (0.0050s) ==========================

== 20200916062631 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0053s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0019s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0040s
== 20200916062631 DeviseCreateUsers: migrated (0.0150s) =======================

== 20200916063812 AddUserIdToPics: migrating ==================================
-- add_column(:pictures, :user_id, :integer)
   -> 0.0048s
== 20200916063812 AddUserIdToPics: migrated (0.0049s) =========================

== 20200916064238 AddUsernameToUsers: migrating ===============================
-- add_column(:users, :username, :string)
   -> 0.0060s
== 20200916064238 AddUsernameToUsers: migrated (0.0062s) ======================

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class AddAttachmentImageToPics < ActiveRecord::Migration[4.2]
/home/sourav/railsProjects/InstagramApp/db/migrate/20200919075557_add_attachment_image_to_pics.rb:1:in `<main>'
/home/sourav/railsProjects/InstagramApp/bin/rails:9:in `<top (required)>'
/home/sourav/railsProjects/InstagramApp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class AddAttachmentImageToPics < ActiveRecord::Migration[4.2]
/home/sourav/railsProjects/InstagramApp/db/migrate/20200919075557_add_attachment_image_to_pics.rb:1:in `<main>'
/home/sourav/railsProjects/InstagramApp/bin/rails:9:in `<top (required)>'
/home/sourav/railsProjects/InstagramApp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

The issue can be fixed simply by modifying the migration file, and add [6.0] beside ActiveRecord::Migration, for me it's something like this:

class AddAttachmentImageToPics < ActiveRecord::Migration[6.0]
  def self.up
    change_table :pictures do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :pics, :image
  end
end

And then run:

rails db:environment:set RAILS_ENV=development db:{drop,create,migrate}

This is a temporary fix that has to be followed every time I create migration...