Delete exchange variants in bulk when deleting an order cycle

This commit is contained in:
Matt-Yorkley
2023-05-18 15:24:36 +01:00
parent 3d1b0816ed
commit 434c9ae110

View File

@@ -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