Soft Delete with Paranoia Discussion
Yes, but paranoia uses default_scope, which many people don't recommend. Here is some good discussion on why: https://stackoverflow.com/q...
Love this gem. We use it on a couple projects at work, and it's been great!
Great episode. I might try to put a method like this in user model to get a user name from deleted user.
I like the idea of using deleted_at to have a timestamp and status at the same time.
Hi Chris, Great video as always.
I'm trying to prevent the users avatar being fully deleted, when the soft delete occurs. I've read documentation to suggest that adding has_attached_file :avatar, preserve_files: true but this doesn't seem to work. I was hoping that you may have an idea why?
Would you be able to give some insight into how to really destroy a record using this gem.
Their documentation says "If you wish to actually destroy an object you may call really_destroy!. WARNING: This will also really destroy all dependent: :destroy records, so please aim this method away from face when using."
So I have it defined in my controller...but what I don't get is when I have the link in my view what method can I use to really destroy it?
Isn't method :destroy just going to try and soft delete it again?
Basically you can take let your normal destroy action call the soft delete method. You'll always want to use that by default.
Then, if you want to add a way to permanently delete it, you can add another route like:
resources :blog_posts do
And then your controller action for this new route can call
Chris, thanks for the reply.
I feel like I am darn close. I just need to get the route right.
Right, so you'd have links to the two different delete urls like this:
<%= link_to "Soft Delete", blog_post_path(@blog_post), method: :delete %>
<%= link_to "Permanently Delete", really_destroy_blog_post_path(@blog_post), method: :delete %>
Hi guys iI need to use this gem but there is an issue with the dependent destroy.
eg: a has many b
paranoid is added to model a
I m able to restore record of a
but i lost all the associated record from model b
Is there a way to soft delete records of model b when I soft delete record of a.
You need to add soft delete on the b model. What I mean is, create a column `deleted_at` (or whatever you want since paranoia supports that option too) and call the class method `acts_as_paranoid` (also if you named your column different than `deleted_at` you should specify it here, just be sure to check the README https://github.com/rubysher... and then the destroy will be recursive on the associations.
What I mean is when you call `a.destroy` if you have `has_many :b, dependent: :destroy` then it will call `b.destroy` on all of the b models associated with model a
Is this gem still the best practice for soft deleting records?
Not any more. This is the new standard https://github.com/jhawthorn/discard