foodsoft/app/models/supplier_category.rb

19 lines
572 B
Ruby
Raw Permalink Normal View History

class SupplierCategory < ActiveRecord::Base
belongs_to :financial_transaction_class
2021-03-05 11:43:03 +01:00
belongs_to :bank_account, optional: true
has_many :suppliers
normalize_attributes :name, :description
validates :name, presence: true, uniqueness: true, length: { minimum: 2 }
before_destroy :check_for_associated_suppliers
protected
# Deny deleting the category when there are associated suppliers.
def check_for_associated_suppliers
raise I18n.t('activerecord.errors.has_many_left', collection: Supplier.model_name.human) if suppliers.undeleted.any?
end
end