class UsdaFood < ApplicationRecord
  include TokenizedLike

  has_many :usda_food_weights

  def self.search(query)
    tokens = query.to_s.gsub(',', '').split(' ')

    if tokens.empty?
      UsdaFood.none
    else
      UsdaFood.matches_tokens(:long_description, tokens)
    end
  end

  def density_best_guess
    density = usda_food_weights.map { |w| w.calculate_density }.compact.first
    density.nil? ? nil : density.to_s
  end

end