From 337c1bcfd41afc3840e51ad12f6acb69603d6f5c Mon Sep 17 00:00:00 2001 From: Damian Legawiec Date: Thu, 16 Jun 2016 08:31:34 +0200 Subject: [PATCH] Fixed: don't destroy dependent associations if we cannot destroy the Country Previously :ensure_not_default callback could stop deleting the Country but associations (with dependent: :destroy) were deleted whatsoever, as this is a rails bug (see rails/rails#3458) --- core/app/models/spree/country.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/country.rb b/core/app/models/spree/country.rb index 163086aa154..814911be055 100644 --- a/core/app/models/spree/country.rb +++ b/core/app/models/spree/country.rb @@ -3,6 +3,10 @@ class Country < Spree::Base has_many :states, dependent: :destroy has_many :addresses, dependent: :restrict_with_error + # we need to have this callback before any dependent: :destroy associations + # https://github.com/rails/rails/issues/3458 + before_destroy :ensure_not_default + has_many :zone_members, -> { where(zoneable_type: 'Spree::Country') }, class_name: 'Spree::ZoneMember', @@ -11,8 +15,6 @@ class Country < Spree::Base has_many :zones, through: :zone_members, class_name: 'Spree::Zone' - before_destroy :ensure_not_default - validates :name, :iso_name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true } def self.default