更新DB /手动更新模型后迁移? [英] Update db/migrate after manually updating models?

查看:102
本文介绍了更新DB /手动更新模型后迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这个模型:

class Product < ActiveRecord::Base
  attr_accessible :name, :order
end

后来,当我做了耙分贝:迁移它创造了这个 DB /迁移/ 20120825132038_create_products.rb

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.integer :order
      t.string :name

      t.timestamps
    end
  end
end

不过,这一切happend因为我使用轨生成产品订单:整数名称:字符串

现在后,我去的产品型号和手动更改它:

Now after i go to Product model and changes it manually to:

class Product < ActiveRecord::Base
  attr_accessible :name, :order, :category_id

  validates :name, uniqueness: true
  belongs_to :category
end

我怎么能自动更新的 DB /迁移/ 20120825132038_create_products.rb 的更新吗?

推荐答案

当你运行耙分贝:迁移,它的没有创建 DB /迁移/ 20120825132038_create_products.rb 。当你跑了,移民文件的创建

When you ran rake db:migrate, it did not create db/migrate/20120825132038_create_products.rb. That migration file was created when you ran

rails generate Product order:integer name:string

attr_accessible 已无关,与迁移数据库。

attr_accessible has nothing to do with migrating your database.

我强烈建议你阅读Rails的手册上的 迁移 的,以及作为上一节的 质量分配 的,其中讨论了 attr_accessible

I strongly recommend you read the Rails Guide on Migrations, as well as the section on Mass Assignment which discusses attr_accessible.

要生成的的迁移文件中的(因为在你的问题中提到的,有一个已经被处理了previous 耙分贝:迁移命令你所提到的运行)的,执行

To generate a new migration file (since the one mentioned in your Question has already been processed by the previous rake db:migrate command you mentioned running), run

rails g migration AddCategoryIdToProduct category_id:integer

这应该生成与内容的新移民一样

This should generate a new migration with contents like

class AddCategoryIdToProduct < ActiveRecord::Migration
  def change
    add_column :products, :category_id, :integer
  end
end

运行耙分贝:迁移再现在处理这个迁移文件,添加新的 CATEGORY_ID 整数列到你产品表。

Running rake db:migrate again now will process this migration file, adding the new category_id integer column to your products table.

这篇关于更新DB /手动更新模型后迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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