Don't delete your records, move them to a different table.
acts_as_paranoid, but doesn't mess with your SQL queries.
gem install acts_as_archive
acts_as_archive to your models:
class Article < ActiveRecord::Base acts_as_archive end
Next time you run
rake db:migrate, your archive tables will be created automatically.
delete_all like you normally would.
Records move into the archive table instead of being destroyed.
New migrations are automatically applied to the archive table.
No action is necessary on your part.
::Archive to your ActiveRecord class:
Use any of the destroy methods, but add a bang (!):
Article::Archive.find(:first).destroy! Article.delete_all!([ "id in (?)", [ 1, 2, 3 ] ])
Use any of the destroy/delete methods on the archived record to move it back to its original table:
Article::Archive.first.destroy Article::Archive.delete_all([ "id in (?)", [ 1, 2, 3 ] ])
You will find an extra
deleted_at datetime column on the archive table.
You may manually add a
restored_at datetime column to the origin table if you wish to store restoration time as well.
Add this line to a migration, or run it via
This copies all records with non-null
deleted_at values to the archive.