We should use acts_as_paranoid recursively based on requirement.

First of all I would like to inform that [acts_as_paranoid gem][1] is not updated for rails 4.

So in our project we have used [paranoia gem][2] which is similar to acts_as_paranoid.

Few days ago we faced a issue like once I archived a object then the associated object also deleted because of dependent: :destroy and I was recovering archived object but all details with the associated objects were lost.

Example: We had model structure like this

class Person
 has_one :assets, as: :attachable, dependent: :destroy
 has_many :members, dependent: :destroy
 has_many :companies, through: :members, dependent: :destroy
class Company
 has_many :assets, as: :attachable, dependent: :destroy
 has_many :members, dependent: :destroy
 has_many :people, through: :members, dependent: :destroy
class Member
 #some required attributes for person and company
 belongs_to :company
 belongs_to :person
class Asset
 belongs_to :attachable, polymorphic: true

So to fix this issue we removed dependent: :destroy for all association.

But one more scenario was arises here. Like when I archived a person then the associated member object not deleted, then I tried access a company that have member record for that archived person. Then of course it would break.

So in this scenario we are using acts_as_paranoid for Member model also and keep dependent: :destroy with association.

So If I archive a person/company then associated member also archived automatically and to recover, paranoia provides a method Person.restore(id, recursive: true), like recover in acts_as_paranoid. So it will recover all associated archived objects as well.

One more advantage of paranoia is if you want to restore multiple records then you can use

Person.restore([id1, id2, ..., idN])

For more about [paranoia click here][3]
[1]: https://github.com/goncalossilva/acts_as_paranoid
[2]: https://github.com/radar/paranoia
[3]: https://github.com/radar/paranoia

Sunil Sharma

I am a Ruby on Rails developer, passionate about building interesting and innovative web and mobile apps. | Ruby on Rails | Ember.js | Cordova | Travelling | Party | Trekking ...

Read More