There are many times where you don't want to permanently delete a record from the database, however you don't want to build an archiving system manually, that's where gems such as Paranoia come in.
Here is how to set it up.
Add the gem to your Gemfile and run bundle:
Now you will need to add a migration to have a deleted_at column for the models that you want to have the archiving functionality. If you want to add it to a model such as Post, you can build the migration like this:
This will create a migration file with the correct parameters, after that you can run rails db:migrate to add the column to the table.
Now all you have to do is add the method call acts_as_paranoid to the model that you want the functionality added to, like this:
acts_as_paranoid
end
Now if you delete a record in the application (using the standard destroy method) it will appear to be removed from the database. However if you run the query:
You'll see that the record is safe and sound. You can also view all of the posts with the query:
And you'll see the archived and live records.
If you want to unarchvie a record you can run the code:
And it will make the record active again.