2010-06-11 4 views
0

시나리오를 상상해 보면 성분이 텍스트 인 조리법 목록이 있습니다.검색 논리를 사용하여 구문을 검색하려면 어떻게합니까?

"참기름"이 들어있는 요리법을 몇 개 보려합니다.

Recipe.ingredients_like ("참기름")을 사용하는 기본 searchlogic 검색의 문제점은 조리법이 어려울 때 "참기름"을 검색 할 때 참깨와 기름을 가진 모든 조리법이 나타납니다. ("참깨"+ 이것은이 sesame또는oil를 포함하는 조리법을 반환합니다 sesame oil (두 개의 공백으로 구분 된 단어)

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

를 포함하는 조리법을 반환합니다

+0

또한 [하나 개의 필드에 여러 단어를 사용 attribute_like_any로 검색]에 관심이있을 수 있습니다 (http://stackoverflow.com/questions/3639818/search-by-attribute-like-any-using-multiple-words-in-one-field-searchlogic) :) –

답변

1

"옥수수 기름"같은 것들을 포함 이들 중 하나 단어)

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%') 
관련 문제