Rails开发与Ruby语言入门指南
1. Rails应用开发基础操作
在Rails开发中,当我们能在浏览器中看到模型展示后,可进一步为其添加更多字段,让应用更丰富。若要添加或修改数据库字段,需使用迁移(migration)来实现。
1.1 添加字段到数据库表
以向 articles 表添加 excerpt 和 location 字段为例,操作步骤如下:
1. 使用迁移生成器创建迁移文件:
$ rails generate migration add_excerpt_and_location_to_articles excerpt:string location:string
执行该命令后,会在 db/migrate 目录下生成一个以时间戳为前缀的迁移文件,如 20130423232337_add_excerpt_and_location_to_articles.rb 。
2. 打开生成的迁移文件,内容如下:
class AddExcerptAndLocationToArticles < ActiveRecord::Migration
def change
add_column :articles, :excerpt, :string
add_column :articles, :locati
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



