mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Delete exchange variants in bulk when deleting an order cycle
This commit is contained in:
@@ -16,7 +16,7 @@ class Exchange < ApplicationRecord
|
||||
belongs_to :sender, class_name: 'Enterprise'
|
||||
belongs_to :receiver, class_name: 'Enterprise'
|
||||
|
||||
has_many :exchange_variants, dependent: :destroy
|
||||
has_many :exchange_variants, dependent: :delete_all
|
||||
has_many :variants, through: :exchange_variants
|
||||
|
||||
has_many :exchange_fees, dependent: :destroy
|
||||
@@ -25,6 +25,8 @@ class Exchange < ApplicationRecord
|
||||
validates :order_cycle, :sender, :receiver, presence: true
|
||||
validates :sender_id, uniqueness: { scope: [:order_cycle_id, :receiver_id, :incoming] }
|
||||
|
||||
before_destroy :delete_related_exchange_variants, prepend: true
|
||||
|
||||
after_save :touch_receiver
|
||||
|
||||
accepts_nested_attributes_for :variants
|
||||
@@ -118,4 +120,11 @@ class Exchange < ApplicationRecord
|
||||
variant_ids.map{ |variant_id| { variant_id: variant_id, exchange_id: exchange_id } }
|
||||
)
|
||||
end
|
||||
|
||||
def delete_related_exchange_variants
|
||||
ExchangeVariant.where(variant_id: variant_ids).
|
||||
joins(:exchange).
|
||||
where(exchanges: { order_cycle: order_cycle, incoming: false }).
|
||||
delete_all
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user