我有这张桌子:

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"列应该称为"Season_id".我知道我必须像在http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers中解释的那样编写‘.rename:season,:season_id’,但是我找不到正确的语法.应该是吗?

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控制台中做什么吗?谢谢!

推荐答案

在控制台中运行:

$ rails g migration rename_season_to_season_id

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

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.

Database相关问答推荐

如何在 ubuntu 中使用脚本添加带有连字符的数据库名称

如何让 Google Drive Electron 表格像 MySQL 数据库一样工作吗?

您是否遇到过 SQL Server 因为引用了太多表而无法执行的查询?

哪个能够存储 1 亿条记录的嵌入式数据库具有高效的 C 或 C++ API

MongoDB和Redis有什么区别?

递归关系的数据库设计

聊天的数据库模式:私有和群组

friendship数据库模式

为 Java servlet 管理数据库连接的最佳方法

如何使用 MySQL Workbench 架构差异两个数据库?

我应该将用户 ID 公开吗?

用于存储文件夹系统的数据库模式的 Select

关系未更新的 NSFetchedResultsController

ManyToOneRel 和 ForeignKey 的区别?

由多个用户编辑数据库记录

SQLite3 数据库的最大连接数是多少?

xampp phpmyadmin,格式参数不正确

Web 应用程序的文件存储:文件系统、数据库和 NoSQL 引擎

是否有用于 postgresql 的数据可视化工具,它也能够显示模式间关系?

获取 SQL Server 2008 中新插入行的主键