Ask A Question

Notifications

You’re not receiving notifications from this thread.

Soft Delete with Paranoia Discussion

Discussion for Soft Delete with Paranoia

Great episode. Thanks, Chris!

Reply

Whcih Gem do you use for the Error and Console on the Browser?

Reply

its better_errors gem, he has already done an episode about it (https://gorails.com/episode...

Reply

Yes, but paranoia uses default_scope, which many people don't recommend. Here is some good discussion on why: https://stackoverflow.com/q...

Reply

Correct for most cases, but when you don't want to expose any deleted records, default_scope is perfect for this.

Reply

Love this gem. We use it on a couple projects at work, and it's been great!

Reply

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!

Reply

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

Reply

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?

Reply

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?

Reply

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!

Reply

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.

Reply
Ristovski Vlatko Ristovski Vlatko

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

Reply

Is this gem still the best practice for soft deleting records?

Reply

I still use it for most things, but there's also https://github.com/jhawthorn/discard

Reply
Reply

@ya na yandex thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.