Is there a better version of something like acts_as_paranoid for Rails 3?
-
28-10-2019 - |
سؤال
Does anyone know of a gem that both soft deletes records from a database (just flags them as deleted as opposed to is actually deleted) and...
...also when do you any type of find automatically omits those trashed records without using any special user-defined scopes. E.g. it all takes place in the gem.
Cheers!
المحلول
You might want to checkout paper_trail
نصائح أخرى
DHH is a fan of doing this with 'concerns' which are basically just module mixins and I like that minimalist approach.
Here's DHH's version - https://gist.github.com/1014971
Here's my bugfixed version - https://gist.github.com/4032984
paper_trail has lots of features and is well tested (been around for a long time).
If you want just the specified functionality, check out:
Paranoia
The Paranoia gem from the overtly talented Radar is what you'd be looking for.
It's essentially a rewrite of acts_as_paranoid (and uses many of the same conventions) but is smaller, faster and compatible with Rails 3 and 4.
It accomplishes both of your requests:
Automatically sets a
deleted_at
timestamp when adestroy
method is called on an object whose class includesacts_as_paranoid
.Automatically sets a condition on any queries that ensure
deleted_at IS NULL
, unless you includewith_deleted
in your query chain.
Couldn't say enough good things about the library or Radar.
Bonus: His documentation might be the best in the "business".
I use Discard gem. It's a quite simple way for soft deletion records. https://www.rubydoc.info/gems/discard/1.0.0