Merge pull request #41025 from uxxman/destroy_async_sti_fix

Handle STI models for has_many dependent: :destroy_async

Fixes #41008.
This commit is contained in:
Ryuta Kamizono 2021-01-06 10:34:49 +09:00 committed by GitHub
commit 7f0bbed4b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 1 deletions

View File

@ -42,7 +42,7 @@ module ActiveRecord
enqueue_destroy_association(
owner_model_name: owner.class.to_s,
owner_id: owner.id,
association_class: association_class.to_s,
association_class: reflection.klass.to_s,
association_ids: ids,
association_primary_key_column: primary_key_column,
ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)

View File

@ -134,6 +134,16 @@ class DestroyAssociationAsyncTest < ActiveRecord::TestCase
end
end
test "has_many with sti parent class destroys all children class records" do
book = BookDestroyAsync.create!
LongEssayDestroyAsync.create!(book: book)
ShortEssayDestroyAsync.create!(book: book)
book.destroy
assert_difference -> { EssayDestroyAsync.count }, -2 do
perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob
end
end
test "enqueues the has_many to be deleted with custom primary key" do
dl_keyed_has_many = DlKeyedHasMany.new

View File

@ -4,3 +4,9 @@ class EssayDestroyAsync < ActiveRecord::Base
self.table_name = "essays"
belongs_to :book, dependent: :destroy_async, class_name: "BookDestroyAsync"
end
class LongEssayDestroyAsync < EssayDestroyAsync
end
class ShortEssayDestroyAsync < EssayDestroyAsync
end

View File

@ -408,6 +408,7 @@ ActiveRecord::Schema.define do
end
create_table :essays, force: true do |t|
t.string :type
t.string :name
t.string :writer_id
t.string :writer_type