Skip to content

Commit 337c1bc

Browse files
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)
1 parent 26fa2d2 commit 337c1bc

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

core/app/models/spree/country.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ class Country < Spree::Base
33
has_many :states, dependent: :destroy
44
has_many :addresses, dependent: :restrict_with_error
55

6+
# we need to have this callback before any dependent: :destroy associations
7+
# https://github.com/rails/rails/issues/3458
8+
before_destroy :ensure_not_default
9+
610
has_many :zone_members,
711
-> { where(zoneable_type: 'Spree::Country') },
812
class_name: 'Spree::ZoneMember',
@@ -11,8 +15,6 @@ class Country < Spree::Base
1115

1216
has_many :zones, through: :zone_members, class_name: 'Spree::Zone'
1317

14-
before_destroy :ensure_not_default
15-
1618
validates :name, :iso_name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
1719

1820
def self.default

0 commit comments

Comments
 (0)