1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/models/book_destroy_async.rb
George Claghorn 4cf7559280 Destroy associations in a background job.
Sometimes cascading association deletions can cause timeouts due to
an IO issue. Perhaps a model has associations that are destroyed on
deletion which in turn trigger other deletions and this can continue
down a complex tree. Along this tree you may also hit other IO
operations. Such deep deletions can lead to server timeouts while
awaiting completion and really the user may not notice all the
changes on their side immediately making them wait unnecesarially or
worse causing a timeout during the operation.

We now allow associations supporting the `dependent:` key to take `:destroy_async`,
which schedules a background job to destroy associations.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Co-authored-by: Cory Gwin @gwincr11 <gwincr11@github.com>
2020-09-24 14:24:15 -04:00

24 lines
737 B
Ruby

# frozen_string_literal: true
class BookDestroyAsync < ActiveRecord::Base
self.table_name = "books"
has_many :taggings, as: :taggable, class_name: "Tagging"
has_many :tags, through: :taggings, dependent: :destroy_async
has_many :essays, dependent: :destroy_async, class_name: "EssayDestroyAsync", foreign_key: "book_id"
has_one :content, dependent: :destroy_async
enum status: [:proposed, :written, :published]
def published!
super
"do publish work..."
end
end
class BookDestroyAsyncWithScopedTags < ActiveRecord::Base
self.table_name = "books"
has_many :taggings, as: :taggable, class_name: "Tagging"
has_many :tags, -> { where name: "Der be rum" }, through: :taggings, dependent: :destroy_async
end