change column name Rails

后端 未结 4 1591
南旧
南旧 2021-02-02 10:18

I have this table:

class CreateShoes < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.boolean :leather
              


        
4条回答
  •  一整个雨季
    2021-02-02 11:16

    Run in your console:

    $ rails g migration rename_season_to_season_id
    

    Now file db/migrate/TIMESTAMP_rename_season_to_season_id.rb contains following:

    class RenameSeasonToSeasonId < ActiveRecord::Migration
      def change
      end
    end
    

    Modify it as follows:

    class RenameSeasonToSeasonId < ActiveRecord::Migration
      def change
        rename_column :shoes, :season, :season_id
      end
    end
    

    Then run $ rake db:migrate in console.

提交回复
热议问题