Commit d99f42d3 by John Hawthorn Committed by John Hawthorn

Fix problem when destroying non-paranoid has_one association

Thanks @kidlab
parent 9b8158e2
......@@ -170,15 +170,16 @@ class ActiveRecord::Base
reflection.options[:dependent] == :destroy
end
if dependent_reflections.any?
dependent_reflections.each do |name, _|
associated_records = self.send(name)
dependent_reflections.each do |name, reflection|
association_data = self.send(name)
# has_one association can return nil
if associated_records && associated_records.respond_to?(:with_deleted)
# Paranoid models will have this method, non-paranoid models will not
associated_records.with_deleted.each(&:really_destroy!)
self.send(name).reload
elsif associated_records && !associated_records.respond_to?(:each) # single record
associated_records.really_destroy!
# .paranoid? will work for both instances and classes
if association_data && association_data.paranoid?
if reflection.collection?
association_data.with_deleted.each(&:really_destroy!)
else
association_data.really_destroy!
end
end
end
end
......
......@@ -361,9 +361,15 @@ class ParanoiaTest < test_framework
def test_real_destroy_dependent_destroy
parent = ParentModel.create
child = parent.very_related_models.create
child1 = parent.very_related_models.create
child2 = parent.non_paranoid_models.create
child3 = parent.create_non_paranoid_model
parent.really_destroy!
refute RelatedModel.unscoped.exists?(child.id)
refute RelatedModel.unscoped.exists?(child1.id)
refute NonParanoidModel.unscoped.exists?(child2.id)
refute NonParanoidModel.unscoped.exists?(child3.id)
end
def test_real_destroy_dependent_destroy_after_normal_destroy
......@@ -757,6 +763,7 @@ class ParentModel < ActiveRecord::Base
has_many :related_models
has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
has_many :non_paranoid_models, dependent: :destroy
has_one :non_paranoid_model, dependent: :destroy
has_many :asplode_models, dependent: :destroy
has_one :polymorphic_model, as: :parent, dependent: :destroy
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment