Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't soft delete ActionText #497

Open
stewartjohnson opened this issue Jul 12, 2020 · 2 comments
Open

Doesn't soft delete ActionText #497

stewartjohnson opened this issue Jul 12, 2020 · 2 comments
Labels

Comments

@stewartjohnson
Copy link

Hi -

I've got a Post model with using ActionText (has_rich_text :body) and I added acts_as_paranoid to it for soft deletes. It works fine on the Post model itself, but the rich text field is automatically deleted regardless. Here's the server log as I deleted a post:

Started DELETE "/posts/16" for 127.0.0.1 at 2020-07-12 21:44:15 +1000
Processing by PostsController#destroy as HTML
  Parameters: {"authenticity_token"=>"qG0f5iaUxM8BNJOHaPkr/xWEeP6AG72yPvy/e4ZFRDRzeIOSCA2QERC6lMYB27rtQickNiDWlZPcBjm70orHwg==", "id"=>"16"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  Post Load (0.1ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`deleted_at` IS NULL AND `posts`.`id` = 16 LIMIT 1
  ↳ app/controllers/posts_controller.rb:50:in `set_post'
  Discussion Load (0.2ms)  SELECT `discussions`.* FROM `discussions` WHERE `discussions`.`id` = 1 LIMIT 1
  ↳ app/controllers/posts_controller.rb:52:in `set_post'
   (0.2ms)  BEGIN
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
  ActionText::RichText Load (0.2ms)  SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 16 AND `action_text_rich_texts`.`record_type` = 'Post' AND `action_text_rich_texts`.`name` = 'body' LIMIT 1
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
  ActiveStorage::Attachment Load (0.3ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 16 AND `active_storage_attachments`.`record_type` = 'ActionText::RichText' AND `active_storage_attachments`.`name` = 'embeds'
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
  ActionText::RichText Destroy (2.2ms)  DELETE FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`id` = 16
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
  Post Update (0.3ms)  UPDATE `posts` SET `posts`.`deleted_at` = '2020-07-12 11:44:16', `posts`.`updated_at` = '2020-07-12 11:44:16.019582' WHERE `posts`.`id` = 16
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
  Post Update (0.2ms)  UPDATE `posts` SET `posts`.`updated_at` = '2020-07-12 11:44:16.019177' WHERE `posts`.`id` = 16
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
   (0.9ms)  COMMIT
  ↳ app/controllers/posts_controller.rb:40:in `destroy'
Redirected to http://localhost:3000/discussions/1
Completed 302 Found in 28ms (ActiveRecord: 4.8ms | Allocations: 25600)

You can see the update of the post model deleted_at column, but there's also the SQL for deleting from the ActionText tables.

Thanks!

@adamzolotarev
Copy link

adamzolotarev commented Aug 5, 2020

Seeing the same issue with ActiveStorage after upgrading to Rails 6.

Come to think about it, it probably is the right behavior, and for some reason up until Rails 6, deleting the model with attached storage just didn't trigger dependent destroy for some reason.

@trkoch
Copy link

trkoch commented Jul 5, 2021

I added an initializer to patch ActionText::RichText and created a migration to add the deleted_at column in action_text_rich_texts.

# Soft delete rich text attributes
Rails.configuration.after_initialize do
  ActionText::RichText.class_eval do
    acts_as_paranoid
  end
end

This is a crude workaround and affects all rich-text attributes. Nevertheless, it did the job for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants