1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +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 committed by Sylvain
parent 19e3921dc4
commit e096d95dcc

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