Skip to content

Commit

Permalink
Add dependent: :destroy to Spree::Order#order_promotions
Browse files Browse the repository at this point in the history
We found a lot of orphaned records in our database due to this missing
dependent: :destroy.
  • Loading branch information
mamhoff committed Oct 3, 2023
1 parent fadebb1 commit 885a62d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def states
foreign_key: :order_id,
dependent: :destroy,
inverse_of: :order
has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
has_many :promotions, through: :order_promotions

# Payments
Expand Down
14 changes: 14 additions & 0 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1818,4 +1818,18 @@ def validate(line_item)
end
end
end

describe "order deletion" do
let(:order) { create(:order) }
let(:promotion) { create(:promotion) }

subject { order.destroy }
before do
order.promotions << promotion
end

it "deletes join table entries when deleting an order" do
expect { subject }.to change { Spree::OrderPromotion.count }.from(1).to(0)
end
end
end

0 comments on commit 885a62d

Please sign in to comment.