Soft Delete with Paranoia Discussion
Whcih Gem do you use for the Error and Console on the Browser?
its better_errors gem, he has already done an episode about it (https://gorails.com/episode...
Yes, but paranoia uses default_scope, which many people don't recommend. Here is some good discussion on why: https://stackoverflow.com/q...
Correct for most cases, but when you don't want to expose any deleted records, default_scope is perfect for this.
Great episode. I might try to put a method like this in user model to get a user name from deleted user.
def name
if deleted_at
'Deleted User'
else
super
end
end
I like the idea of using deleted_at to have a timestamp and status at the same time.
Thanks!
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?
Thanks
Gareth
Hey Gareth, when you do a soft delete, there should be nothing that happens other than a database field called deleted_at getting set. This won't affect images at all because they should only get removed when destroyed. Are the images actually getting removed?
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
member do
delete :really_destroy
end
end
And then your controller action for this new route can call @blog_post.really_destroy!
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?
I still use it for most things, but there's also https://github.com/jhawthorn/discard
Not any more. This is the new standard https://github.com/jhawthorn/discard
Why? https://github.com/jhawthorn/discard#why-not-paranoia-or-acts_as_paranoid