2

I'm trying to get back into RoR after a long hiatus, and was getting an error when I tried to rails db:migrate:

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:

the errors continue...

I'm thinking its because of the gem ratyrate.

In one of the migration files:

class CreateRatingCaches < ActiveRecord::Migration

  def self.up
      create_table :rating_caches do |t|
        t.belongs_to :cacheable, :polymorphic => true
        t.float :avg, :null => false
        t.integer :qty, :null => false
        t.string :dimension
        t.timestamps
      end

      add_index :rating_caches, [:cacheable_id, :cacheable_type]
    end

    def self.down
      drop_table :rating_caches
    end

end

is it because rails 5 don't use def self.up / def self.down? and instead should be using def change?

If that's the case, is it okay for me to just change the def setf.up to def change and then remove the def self.down block?

In addition to this, why is there even a def self.down calling to drop the table when its creating the table? Does it not get executed, only when you db:rollback the database?

Thanks

hellomello
  • 8,219
  • 39
  • 151
  • 297
  • Yes, you change to `def change` and drop the `self.down` method. The `up` and `down` are only necessary when you're doing some custom stuff. – Eyeslandic May 28 '17 at 19:52

1 Answers1

8

Since you are inheriting from ActiveRecord::Migration the migration is cancelled.
You should therefore inherit from ActiveRecord::Migration[5.1] and the migration should work. Change the first line to:

class CreateRatingCaches < ActiveRecord::Migration[5.1]

(5.1 specifies the rails version, adapt acordingly, e.g. ActiveRecord::Migration[4.2] etc.)

See this answer for more information about change vs. up/down in migrations or read this part in the official guide for detail information about the different aspects about them.

Niklas
  • 298
  • 2
  • 7