每天一剂Rails良药之Versioning Your ActiveRecord Models

Wiki有一个功能就是Undo,我们来看看Rails的acts_as_versioned插件

1,安装插件
[code]
ruby script/plugin install acts_as_versioned
[/code]

2,例子
[code]
class Chapter < ActiveRecord::Base
acts_as_versioned
end

class AddChapterAndVersionTables < ActiveRecord::Migration
def self.up
create_table :chapters do |t|
t.column :title, :string
t.column :body, :text
t.column :version, :integer
end
Chapter.create_versioned_table
end

def self.down
drop_table :chapters
Chapter.drop_versioned_table
end
end
[/code]
Chapter.create_versioned_table会为我们自动生成chapter_versions表,该表和chapter是多对一的关系
这样每次修改chapter对象都会生成一个新的version
回退到某一个版本:
[code]
chapter.revert_to(version_number)
[/code]
得到总版本数:
[code]
chapter.versions(true).size
[/code]
具体还有哪些方法看看源码acts_as_versioned.rb即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值