3

In a Rails 4.2 project I use gem 'permanent_records' to handle records soft-deletion and gem 'globalize' for translations.

Globalize sets the following relationships between the translated Model and the Translation Model (source):

has_many :translations, :class_name  => translation_class.name,
                                :foreign_key => options[:foreign_key],
                                :dependent   => :destroy,
                                :extend      => HasManyExtensions,
                                :autosave    => true,
                                :inverse_of  => :globalized_model

The result is that calling :destroy on the translated Model, does not delete it (which is what permanent_records is used for), but I lose the related translations, which I'd like to keep alive.

Shall I override the dependent: :destroy callback only on some translated models (the translated models where I use permanent_records)? How to do it properly?

Is there any other way to get the desired result?

davideghz
  • 3,101
  • 5
  • 19
  • 39

2 Answers2

2

There many ways to handle it. I wouldn't recommend to override dependent: :destroy.

  1. best way to handle it by using paranoia gem which is a re-implementation of acts_as_paranoid. This gem takes care of soft delete with dependent destroy option. Refer: https://github.com/rubysherpas/paranoia

  2. Override callback like this

    def destroy
      run_callbacks :destroy do
        # your code here for destroy
      end
    end
    
davideghz
  • 3,101
  • 5
  • 19
  • 39
Ravi Prakash
  • 123
  • 1
  • 7
  • Thanks for you input. Unfortunately I can't change the used gems. Anyway, as far I see in Paranoia doc, it relies on the dependent: :destroy associations defined in the models. My problem is that those associations are defined by Globalize itself for translated models, so I can really play with it... – davideghz Dec 05 '17 at 09:38
  • Try the second option then – Ravi Prakash Dec 05 '17 at 09:42
1

I finally solved the issue by adding a deleted_at attribute to GlobalizedModel::Translation Model (in my case Treatment::Translation) so that also translations are soft-deleted.

davideghz
  • 3,101
  • 5
  • 19
  • 39