Opt-in ability to soft-delete records in future for gem paranoia
By default Paranoia treats any record with non-null deleted_at
as already deleted record no matter actual value of the attribute. And sometimes you may want to mark records for automatic deletion in the future.
Check not only the paranoia_column
is nil, but also check its value in relation to the current timestamp.
-
Put
paranoia_in_the_future.rb
file into your Ruby on Rails appapp/models/concerns
directory -
Include the
ParanoiaInTheFuture
module to model classes that need to enable this behavior: -
Call
acts_as_paranoid
as always
class FutureProof < ApplicationRecord
include ParanoiaInTheFuture
acts_as_paranoid
end
-
Database query performance may decrease due to
OR
ing of two conditions:SELECT * FROM future_proofs WHERE deleted_at IS NULL OR deleted_at > CURRENT_TIMESTAMP
Your existing indexes may become insufficient. Check your query plans on production-like environment first!
-
Paranoia callbacks will be fired immediately and won't be postponed.
-
To ensure that results are consistent in long-running transactions, current value of standard SQL 1992
CURRENT_TIMESTAMP
function is used for both querying and soft-deleting records, though this adds a bit of complexity and fragility (as ActiveRecord doesn't allow to use SQL literals in theupdate_columns
method I didn't want to change the implementation too much from the original).