Skip to content

Commit

Permalink
Fixed: don't destroy dependent associations if we cannot destroy the …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
damianlegawiec committed Jun 16, 2016
1 parent 26fa2d2 commit 337c1bc
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/app/models/spree/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 337c1bc

Please sign in to comment.