module Ecom module Core class MaterialIdentity < ApplicationRecord belongs_to :material_type belongs_to :material_sub_type, optional: true validates :material_type_id, :material_type, presence: true validate :material_category_hierarchy_validator def material_category_hierarchy_validator return if material_type_id.nil? || material_sub_type_id.nil? material_sub_type = MaterialSubType.find_by(id: material_sub_type_id) return unless material_sub_type.material_type_id != material_type_id errors.add(:material_type, 'The given material_sub_type does not belong to the given material_type') end end end end