# Filtering 'hidden' records

**URL:** <https://www.ruby-forum.com/t/filtering-hidden-records/128718>\
**Category:** Rails\
**Created:** [February 11, 2008, 12:21am UTC](https://www.ruby-forum.com/t/filtering-hidden-records/128718 "2008-02-11T00:21:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 11, 2008, 12:21am UTC](https://www.ruby-forum.com/t/filtering-hidden-records/128718/1 "2008-02-11T00:21:18Z")

</div>

A common pattern I’ve used in database application development is some  
form  
of ‘state’ field that is used to administratively manage records.  
Rather  
than deleting records I will mark them with a state of ‘deleted’ and it  
becomes an administrative task to actually later remove and/or archive  
them  
from the database. That way data never just disappears but there is a  
clear  
audit-trail and archive of all data.

This implies that from an application view the records with a ‘deleted’  
state should disappear as if they are truly deleted. All queries must  
filter out those deleted records as if they don’t exist.

Therefore there should be some filter automatically added to every query  
in  
the rails apps to be sure to exclude those records (WHERE state NOT  
'deleted). I would prefer that this is transparent and that the  
application doesn’t have to always add this filter but that it is  
automatically handled by ActionRecord.

What is the best way to achieve this with Rails? Is there already  
native  
ActionRecord capability for this or perhaps a plugin available for this  
functionality?

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 11, 2008, 1:18am UTC](https://www.ruby-forum.com/t/filtering-hidden-records/128718/2 "2008-02-11T01:18:14Z")

</div>

Thank you, it appears that acts\_as\_paranoid is exactly what I’m looking  
for.

The following statement in the docs, “Most normal model operations will  
work, but there will be some oddities,” does concern me a bit since  
those  
‘oddities’ are not at all defined.

- Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [February 11, 2008, 12:40am UTC](https://www.ruby-forum.com/t/filtering-hidden-records/128718/3 "2008-02-11T00:40:07Z")

</div>

On Feb 11, 2008, at 24:20 , Mark wrote:

> queries must filter out those deleted records as if they don’t exist.
> 
> Therefore there should be some filter automatically added to every  
> query in the rails apps to be sure to exclude those records (WHERE  
> state NOT 'deleted). I would prefer that this is transparent and  
> that the application doesn’t have to always add this filter but that  
> it is automatically handled by ActionRecord.
> 
> What is the best way to achieve this with Rails? Is there already  
> native ActionRecord capability for this or perhaps a plugin  
> available for this functionality?

I think the canonical solution in Rails is acts\_as\_paranoid.

– fxn
