0

I have implemented a safe destroy method for our User model since users can only be hard destroyed unless the user has surveys, otherwise, a soft destroy (by setting deleted_at) is done:

alias_method :original_destroy, :destroy

def destroy
  if surveys.any?
    update_column :deleted_at, DateTime.now
    surveys.each { |s| s.anonymize }
    send :_run_destroy_callbacks
    @destroyed = true
    freeze
  else
    original_destroy
  end
end

I'm not sure whether there's a better way to destroy all associationes with dependent: destroy than the send :_run_destroy_callbacks line above.

Thanks for your hints!

4

1 回答 1

0

我自己来回答这个问题:对于 Rails 4,你应该run_callbacks(:destroy) { ... }改用。

于 2014-06-06T15:46:31.537 回答