更改列名称Rails [英] change column name Rails

查看:73
本文介绍了更改列名称Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张桌子:

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

      t.timestamps null: false
    end
  end
end

季节列应称为 season_id。我知道我必须写 http中所述://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers ,但是我找不到合适的语法。

the 'season' column should be called 'season_id'. I know that I have to write 't.rename :season, :season_id' as explained in http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers but I don't manage to find the right syntax. Should it be?

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

      t.timestamps null: false
    end

    change_table :products do |t|
      t.rename :season, :season_id
    end

  end
end

不起作用。在Mac控制台中我需要做什么?谢谢!

Doesn't work. Anything I have to do in the Mac console? Thanks!

推荐答案

在控制台中运行:

$ rails g migration rename_season_to_season_id

现在的文件 db / migration / TIMESTAMP_rename_season_to_season_id.rb 包含以下内容:

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

class RenameSeasonToSeasonId < ActiveRecord::Migration
  def change
  end
end

修改

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

然后在控制台中运行 $ rake db:migrate

这篇关于更改列名称Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆