2010-06-11 76 views
0

想象一下情况下,你有一份食谱作为文本的配料列表。如何搜索使用搜索逻辑的短语?

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

使用Recipe.ingredients_like(“香油”)的默认搜索searchlogic的问题是,用香油和油任何配方会来,当我搜索“香油”,这是一个问题,当配方可含有像“芝麻” +“玉米油”

+0

您可能也有兴趣[使用多个单词attribute_like_any在一个字段搜索] (http://stackoverflow.com/questions/3639818/search-by-attribute-like-any-using-multiple-words-in-one-field-searchlogic):) – 2010-09-07 22:14:21

回答

1

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

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

食谱这将返回包含sesameoil食谱的东西(这些中的任何一个字)

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

这将返回包含sesameoil(以任意顺序两个单词)食谱

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