Hello!
I am trying to figure out how to implement logical deletes instead of
physical deletes using ActiveRecord. Basically, in many applications
that deal with E-Commerce, you really can’t physically delete almost
anything, because records must be kept for auditing and customer service
tracking purposes.
In the past, I’ve implemented logical deletes as follows:
- Oracle implementation:
Each table that must implement logical deletes has a view, eg:
USERS_RAW => real table, has columns:
is_deleted(boolean,default false)
date_deleted(timestamp, default null)
USERS => is a view on USERS_RAW (where isdeleted = false)
Oracle allows updating such a view without any extra triggers, so from
RoR point of view this is completely transparent.
PostgreSQL allows updating views, but with extra triggers, which is ugly
and not what I would like to do.
- Hibernate Implementation
In hibernate when you map a table you can specify that any query on this
table must also have an extra clause appended to it, so adding
“is_deleted = false” makes it work out of the box (except for primary
key fetches unfortunately, but this can be overriden in a model
superclass).
- Rails Implementation
What I would like is that all finders and by-id look ups automatically
append “is_deleted == false” to all SQL queries for a specific set of
classes (which could subclass eg “LogicallyDeletable” model super
class).
Any pointers on how to implement this elegantly are much appreciated.
Thanks,
Konstantin