Filtering 'hidden' records

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

I think the canonical solution in Rails is acts_as_paranoid.

-- fxn

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