class Ingredient < ApplicationRecord
  self.abstract_class = true

  class << self

    def find_by_ingredient_id(ingredient_id)
      case ingredient_id
        when /^R(\d+)$/
          Recipe.find($1)
        when /^F(\d+)$/
          Food.find($1)
        else
          raise ActiveRecord::RecordNotFound
      end
    end

  end

  def ingredient_id
    case self
      when Recipe
        "R#{id}"
      when Food
        "F#{id}"
      else
        raise 'Unknown ingredient'
    end
  end

  def nutrition_errors
    []
  end

  def density?
    !self.density.nil?
  end

end