0

I have a user model and no soft deletion is enabled for the user model. For one it's associated model(has_many association) say posts, soft deletion is enabled. What I want is to hard delete all the associated posts when a user is deleted.

In User.rb

has_many :posts, dependent: :destroy

Which just soft deletes the associated records. So the exception will be raised when deleting the user. Is there any option to hard delete the associated records in the model level?. Or have to do something in before_destroy callback to achieve this?

Note: Have used paranoia gem for soft deletion

Aarthi
  • 1,451
  • 15
  • 39

1 Answers1

1

If you want to hard-delete the associated posts, you can use dependent: :delete_all. This will go directly to the database to delete the records and bypass the before_destroy callbacks registered by the paranoia gem.

Marcus Ilgner
  • 6,935
  • 2
  • 30
  • 44
  • Sorry we are using acts_as_paranoid gem. The gems doesn't have this behaviour. Do I have to go with `before_destroy` callbacks in that case?. I have checked the acts_as_paranoid gem that doesnt seems to have this option. – Aarthi Mar 20 '19 at 07:06
  • `acts_as_paranoid` should work with `:delete_all`, too. I just checked [the source](https://github.com/ActsAsParanoid/acts_as_paranoid/blob/master/lib/acts_as_paranoid/core.rb#L34) and it doesn't fiddle around with `delete_all` except making sure the default scope isn't getting in the way. Do you have any error messages or so? – Marcus Ilgner Mar 20 '19 at 20:42