migration例子

 1:建表选项
:null => true
:limit=> size
:default => value

ruby 代码
 
  1. class CreateUsers < ActiveRecord::Migration  
  2.   def self.up  
  3.     create_table :users do |t|  
  4.       t.column :login:string  
  5.       t.column :email:string  
  6.       t.column :password:string  
  7.       t.column :created_at:datetime  
  8.       t.column :last_login_at:datetime  
  9.       t.column :admin:boolean  
  10.       t.column :posts_count:integer:default => 0  
  11.     end  
  12.   end  
  13.   
  14.   def self.down  
  15.     drop_table :users  
  16.   end  
  17. end  
2:添加 字段
ruby 代码
 
  1. class AddForumDesc < ActiveRecord::Migration  
  2.   def self.up  
  3.     add_column "forums""description":string  
  4.   end  
  5.   
  6.   def self.down  
  7.     remove_column "forums""description"  
  8.   end  
  9. end  

3:修改 数据

ruby 代码
 
  1. class SetLastSeenAt < ActiveRecord::Migration  
  2.   def self.up  
  3.     User.find(:all).each do |user|  
  4.       if user.last_seen_at.nil?  
  5.         user.last_seen_at=Time.now.utc  
  6.         user.save!  
  7.       end  
  8.     end  
  9.   end  
  10.   
  11.   def self.down  
  12.   end  
  13. end  

4用sql改变 数据

ruby 代码
 
  1. class FixRepliedAt < ActiveRecord::Migration  
  2.   def self.up  
  3.     execute 'update posts set replied_at=created_at where replied_at is null and id=topic_id'  
  4.   end  
  5.   
  6.   def self.down  
  7.   end  
  8. end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值