ITeye博客

migration例子

hqman · 2007-12-29 00:44 · ROR · Ruby · SQL · UP · ActiveRecord · Markdown

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. def self.down
  14. drop_table :users
  15. end
  16. end

2:添加 字段

ruby 代码

 

  1. class AddForumDesc < ActiveRecord::Migration
  2. def self.up
  3. add_column "forums", "description", :string
  4. end
  5. def self.down
  6. remove_column "forums", "description"
  7. end
  8. 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. def self.down
  11. end
  12. 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. def self.down
  6. end
  7. end

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。