1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

reset product_category_id to nil if product_category is removed

This commit is contained in:
Du Peng 2022-07-25 11:16:41 +02:00
parent 00f811645a
commit d690db8b6f

View File

@ -7,7 +7,13 @@ class ProductCategoryService
end
def self.destroy(product_category)
ProductCategory.where(parent_id: product_category.id).destroy_all
product_category.destroy
ActiveRecord::Base.transaction do
sub_categories = ProductCategory.where(parent_id: product_category.id)
# remove product_category and sub-categories related id in product
Product.where(product_category_id: sub_categories.map(&:id).push(product_category.id)).update(product_category_id: nil)
# remove all sub-categories
sub_categories.destroy_all
product_category.destroy
end
end
end