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 ...@@ -170,15 +170,16 @@ class ActiveRecord::Base
reflection.options[:dependent] == :destroy reflection.options[:dependent] == :destroy
end end
if dependent_reflections.any? if dependent_reflections.any?
dependent_reflections.each do |name, _| dependent_reflections.each do |name, reflection|
associated_records = self.send(name) association_data = self.send(name)
# has_one association can return nil # has_one association can return nil
if associated_records && associated_records.respond_to?(:with_deleted) # .paranoid? will work for both instances and classes
# Paranoid models will have this method, non-paranoid models will not if association_data && association_data.paranoid?
associated_records.with_deleted.each(&:really_destroy!) if reflection.collection?
self.send(name).reload association_data.with_deleted.each(&:really_destroy!)
elsif associated_records && !associated_records.respond_to?(:each) # single record else
associated_records.really_destroy! association_data.really_destroy!
end
end end
end end
end end
......
...@@ -361,9 +361,15 @@ class ParanoiaTest < test_framework ...@@ -361,9 +361,15 @@ class ParanoiaTest < test_framework
def test_real_destroy_dependent_destroy def test_real_destroy_dependent_destroy
parent = ParentModel.create 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! 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 end
def test_real_destroy_dependent_destroy_after_normal_destroy def test_real_destroy_dependent_destroy_after_normal_destroy
...@@ -757,6 +763,7 @@ class ParentModel < ActiveRecord::Base ...@@ -757,6 +763,7 @@ class ParentModel < ActiveRecord::Base
has_many :related_models has_many :related_models
has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
has_many :non_paranoid_models, dependent: :destroy has_many :non_paranoid_models, dependent: :destroy
has_one :non_paranoid_model, dependent: :destroy
has_many :asplode_models, dependent: :destroy has_many :asplode_models, dependent: :destroy
has_one :polymorphic_model, as: :parent, dependent: :destroy has_one :polymorphic_model, as: :parent, dependent: :destroy
end 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