2016-01-24 17:10:43 -06:00
|
|
|
class UsdaFood < ActiveRecord::Base
|
2016-01-28 14:19:51 -06:00
|
|
|
include TokenizedLike
|
|
|
|
|
2016-03-09 18:53:47 -06:00
|
|
|
has_many :usda_food_weights
|
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
def self.search(query)
|
|
|
|
tokens = query.to_s.split(' ')
|
|
|
|
|
|
|
|
if tokens.empty?
|
|
|
|
UsdaFood.none
|
|
|
|
else
|
2016-01-28 18:18:45 -06:00
|
|
|
UsdaFood.matches_tokens(:long_description, tokens)
|
2016-01-28 14:19:51 -06:00
|
|
|
end
|
|
|
|
end
|
2016-01-24 17:10:43 -06:00
|
|
|
|
2016-03-09 20:12:38 -06:00
|
|
|
def density_best_guess
|
2016-03-10 16:06:39 -06:00
|
|
|
density = usda_food_weights.map { |w| w.calculate_density }.compact.first
|
|
|
|
density.nil? ? nil : density.to_s
|
2016-03-09 20:12:38 -06:00
|
|
|
end
|
|
|
|
|
2016-01-24 17:10:43 -06:00
|
|
|
end
|