在rails数据库中更改列名。

时间:2012-02-24 19:15:51

标签: ruby-on-rails database rename

我有这张桌子

class CreateEvents < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
      t.integer :subcategory
      t.string :event_name
      t.text :description
      t.string :location
      t.date :date
      t.decimal :price

          t.timestamps
            end
          end

      def self.down
        drop_table :events
      end
end

我希望将子类别更改为subcategory_id。我尝试了这个,但没有工作

ruby​​脚本/生成迁移RenameDatabaseColumn,然后我转到db \ migrate中的文件并编辑为这样

class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subgategory, :subgategory_id
end

def self.down
 # rename back if you need or do something else or do nothing
end
end

然后我运行命令rake db:migrate put列仍然是子类别。你能帮我吗?我正在使用rails 2.0

谢谢

1 个答案:

答案 0 :(得分:11)

你拼错了列名吗?不是:subcategory吗?你写了:subgategory

class RenameDatabaseColumn < ActiveRecord::Migration
  def self.up
    rename_column :events, :subcategory, :subcategory_id
  end

  def self.down
    # rename back if you need or do something else or do nothing
  end
end