From 41ffddbc8b7faec66a26bd48dfd36a9def6cc23f Mon Sep 17 00:00:00 2001 From: Laerti Date: Tue, 15 Jan 2019 15:03:20 +0100 Subject: =?UTF-8?q?Refs=20#28025=20nullify=20*=5Ftype=20=20column=20on=20p?= =?UTF-8?q?olymorphic=20associations=20on=20:nu=E2=80=A6=20(#28078)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR addresses the issue described in #28025. On `dependent: :nullify` strategy only the foreign key of the relation is nullified. However on polymorphic associations the `*_type` column is not nullified leaving the record with a NULL `*_id` but the `*_type` column is present. --- activerecord/lib/active_record/associations.rb | 6 ++++-- activerecord/lib/active_record/associations/foreign_association.rb | 7 +++++++ .../lib/active_record/associations/has_many_association.rb | 2 +- activerecord/lib/active_record/associations/has_one_association.rb | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index fb1df00dc8..7bdbd8ce69 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1293,7 +1293,8 @@ module ActiveRecord # # * :destroy causes all the associated objects to also be destroyed. # * :delete_all causes all the associated objects to be deleted directly from the database (so callbacks will not be executed). - # * :nullify causes the foreign keys to be set to +NULL+. Callbacks are not executed. + # * :nullify causes the foreign keys to be set to +NULL+. Polymorphic type will also be nullified + # on polymorphic associations. Callbacks are not executed. # * :restrict_with_exception causes an ActiveRecord::DeleteRestrictionError exception to be raised if there are any associated records. # * :restrict_with_error causes an error to be added to the owner if there are any associated objects. # @@ -1436,7 +1437,8 @@ module ActiveRecord # # * :destroy causes the associated object to also be destroyed # * :delete causes the associated object to be deleted directly from the database (so callbacks will not execute) - # * :nullify causes the foreign key to be set to +NULL+. Callbacks are not executed. + # * :nullify causes the foreign key to be set to +NULL+. Polymorphic type column is also nullified + # on polymorphic associations. Callbacks are not executed. # * :restrict_with_exception causes an ActiveRecord::DeleteRestrictionError exception to be raised if there is an associated record # * :restrict_with_error causes an error to be added to the owner if there is an associated object # diff --git a/activerecord/lib/active_record/associations/foreign_association.rb b/activerecord/lib/active_record/associations/foreign_association.rb index 40010cde03..59af6f54c3 100644 --- a/activerecord/lib/active_record/associations/foreign_association.rb +++ b/activerecord/lib/active_record/associations/foreign_association.rb @@ -9,5 +9,12 @@ module ActiveRecord::Associations false end end + + def nullified_owner_attributes + Hash.new.tap do |attrs| + attrs[reflection.foreign_key] = nil + attrs[reflection.type] = nil if reflection.type.present? + end + end end end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index f6fdbcde54..eb22db838c 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -92,7 +92,7 @@ module ActiveRecord if method == :delete_all scope.delete_all else - scope.update_all(reflection.foreign_key => nil) + scope.update_all(nullified_owner_attributes) end end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 390bfd8b08..99971286a3 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -33,7 +33,7 @@ module ActiveRecord target.destroy throw(:abort) unless target.destroyed? when :nullify - target.update_columns(reflection.foreign_key => nil) if target.persisted? + target.update_columns(nullified_owner_attributes) if target.persisted? end end end -- cgit v1.2.3