class FoodUnit < ApplicationRecord
  belongs_to :food, inverse_of: :food_units

  validates :name, presence: true
  validates :gram_weight, presence: true

  def matches?(unit)
    case
      when unit.empty? && ['each', 'ech', 'item', 'per'].include?(self.name.downcase)
        true
      when unit.downcase.singularize == self.name.downcase
        true
      else
        false
    end
  end
end