更新到最新版本:
rake db:migrate
重设数据库:
rake db:migrate VERSION=0
字段操作
1、字段类型
:binary, :boolean, :date, :datetime, :float, :integer, :string, :text, :time, :timestamp
2、add_column 添加字段
参数
:null => true or false 是否可为null
:limit => size 字段大小,通常是string字段的长度
:default => value 缺省的值
-
add_column
:orders
,
:placed_at
,
:datetime
,
:default
=>
Time
.now
3、rename_column 字段名修改
-
class
RenameEmailColumn < ActiveRecord::Migration
-
def
self
.up
-
rename_column
:orders
,
:e_mail
,
:customer_email
-
end
-
def
self
.down
-
rename_column
:orders
,
:customer_email
,
:e_mail
-
end
-
end
4、change_column 字段类型属性修改
-
def
self
.up
-
change_column
:orders
,
:order_type
,
:string
,
:null
=>
false
-
end
-
def
self
.down
-
change_column
:orders
,
:order_type
,
:integer
-
end
表操作
-
class
CreateOrderHistories < ActiveRecord::Migration
-
def
self
.up
-
create_table
:order_histories
do
|t
|
-
t.column
:order_id
,
:integer
,
:null
=>
false
-
t.column
:created_at
,
:timestamp
-
t.column
:notes
,
:text
-
end
-
end
-
def
self
.down
-
drop_table
:order_histories
-
end
-
end
重命名rename_table
索引
-
class
AddCustomerNameIndexToOrders < ActiveRecord::Migration
-
def
self
.up
-
add_index
:orders
,
:name
-
end
-
def
self
.down
-
remove_index
:orders
,
:name
-
end
-
end
PK
-
create_table
:tickets
,
:primary_key
=>
:ticket_number
do
|t
|
-
t.column
:created_at
,
:timestamp
-
t.column
:description
,
:text
-
end
raise ActiveRecord::IrreversibleMigration 不可逆错误
-
class
ChangeOrderTypeToString < ActiveRecord::Migration
-
def
self
.up
-
change_column
:orders
,
:order_type
,
:string
,
:null
=>
false
-
end
-
def
self
.down
-
raise
ActiveRecord::IrreversibleMigration
-
end
-
end
分享到:
相关推荐
RoR学习的最好参考手册,api参考手册,功能详细,论述清晰,是学习rails框架的不可缺少的手册
《Rails之道》按照Rails的各个子系统进行组织编排,分别介绍了Rails的环境、初始过程、配置和日志记录,Rails的分配器、控制器... 《Rails之道》是Rails的权威参考书,适合对Rails已经有一定了解的开发人员学习和使用。
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
NULL 博文链接:https://thinking80s.iteye.com/blog/424942
Spring是Rails的预加载器。Spring 能够通过保持应用运行在后台,来提升开发模式下的速度。使得不需要在执行test,rake task以及migration时每次都加载。 标签:Spring
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
rails文档 rails api 英文
rails指南 中文版
Ruby+on+Rails+3+Tutorial.pdf 应用Rails进行敏捷Web开发第4版.pdf (Agile Web Development with Rails) Rails.Recipes.Rails.3.Edition.pdf