jhawthorn / discard

🃏🗑 Soft deletes for ActiveRecord done right

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Will this work when saving related models?

bo-oz opened this issue · comments

commented

I'm considering this gem for a project, but have some hard time understanding how it would impact a set-up in which I'm using accept_nested_attributes for on a model. Could I overwrite the default behavior of that to use discard insstead of destroy when updating the join table?

commented

It seems that you have to write a callback that throws abort on the destroy and call discard instead. Could be nice if this was part of the gem. Happy to create a PR for this, but I don't know how to make it in a way that it's optional. Any ideas?

class UserChannel < ApplicationRecord
  include Discard::Model
  before_destroy :force_soft_delete

  belongs_to :user
  belongs_to :channel

  private

  def force_soft_delete
    self.discard
    puts self
  end

The gem could support it. I'm having problems trying to soft delete a resource through parent with nested attributes and I don't think that changing the behaviour of destroy is a good solution.

commented

I agree, ran into issues further down the road. It’s a big miss though that nested models aren’t supported. I think the popular alternatives do... but I prefer the straightforwardness of this implementation.

I solved this changing the destroy method (it's against the gem purpose but there's no support for soft deleting through nested attributes)...

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def destroy
    respond_to?(:discarded_at) ? discard : super
  end
end

What you're describing is what paranoia does. It overrides ActiveRecord methods to factor in soft deletion.