---
title: "用acts_as_paranoid 做\"假删除\""
author: "fireflyman"
date: "2011-01-07T03:11:07Z"
tags: []
categories: ["编程语言"]
slug: "acts-as-paranoid"
draft: false
lastmod: "2011-01-07T03:11:29Z"
id: 862788
url: "https://fireflyman.iteye.com/blog/862788"
---

针对电子商务网站,订单删除后如果无法恢复,则会造成极大影响,因此我们在处理此类数据的时候,不应该直接删除.可考虑使用acts_as_paranoid来做假删除.方法很简单:

插件地址:<https://github.com/technoweenie/acts_as_paranoid>

(1)安装插件,并未需要使用假删除的表加入deleted_at字段,如下-->


```Ruby
script/plugin install git://github.com/technoweenie/acts_as_paranoid.git
script/generate migration add_deleted_at_to_event deleted_at:datetime
rake db:migrate
```


(2)在对应的模型里加入-->


```Ruby
class Event < ActiveRecord::Base
  acts_as_paranoid
end

```

加上后,调用这个模型的destroy方法将不会真正地删除记录,只会将记录从视图上移除,并且在deleted_at里记录当前的时间.当然,你可以在find中使用with_deleted或only_deleted参数得到被隐藏的记录.