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:
parent
19e3921dc4
commit
e096d95dcc
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user