0

Imagine case scenario, you have a list of recipes that have ingredients as a text.

You want to see how many recipes contain "sesame oil".

The problem with default searchlogic searching using Recipe.ingredients_like("sesame oil") is that any recipe with sesame AND oil would come up, when I'm searching for "sesame oil" which is a problem when the recipe may contain things like "sesame seeds" + "corn oil"

fivetwentysix
  • 7,379
  • 9
  • 40
  • 58
  • You might also be interested in [Search by attribute_like_any using multiple words in one field](http://stackoverflow.com/questions/3639818/search-by-attribute-like-any-using-multiple-words-in-one-field-searchlogic) :) – maček Sep 07 '10 at 22:14

1 Answers1

1

This will return Recipes that contain sesame oil (two words separated by a space)

Recipe.ingredients_like("sesame oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame oil%') 

This will return Recipes that contain sesame or oil (any one of these words)

Recipe.ingredients_like_any("sesame", "oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame%' OR recipes.ingredients LIKE '%oil%') 

This will return Recipes that contain sesame and oil (both words in any order)

Recipe.ingredients_like_all("sesame", "oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame%' AND recipes.ingredients LIKE '%oil%') 
maček
  • 76,434
  • 37
  • 167
  • 198