0

想象一下案例场景,您有一个食谱列表,其中包含作为文本的成分。

你想看看有多少食谱含有“芝麻油”。

使用 Recipe.ingredients_like("sesame oil") 进行默认搜索逻辑搜索的问题是,当我搜索“芝麻油”时,任何含有芝麻和油的食谱都会出现,这在食谱可能包含“芝麻”+“玉米油”

4

1 回答 1

1

这将返回包含sesame oil(用空格分隔的两个单词)的食谱

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

这将返回包含sesame oil(这些词中的任何一个)的食谱

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

这将返回包含sesame oil(以任何顺序的两个单词)的食谱

Recipe.ingredients_like_all("sesame", "oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame%' AND recipes.ingredients LIKE '%oil%') 
于 2010-09-03T07:47:24.400 回答