Using Rails and MySQL. Not sure if this has been asked, but couldn’t find it.
I have the following records:
1. I love Rails
2. I love Django
3. I code using Ruby on Rails
====
(1) Search: “i love”
Results: “I love Rails”, “I love Django”
====
(2) Search: “love”
Results: “I love Rails”, “I love Django”
====
(3) Search: “ails”
Results: “I love Rails”, “I code using Ruby on Rails”
====
(4) Search: “code Ruby”
Results: “I code using Ruby on Rails”
====
My code is:
term = "%#{params[:term]}%"
shops = Shop.where("name LIKE ?", term)
It only works on Search (1) and (2). How can I code to achieve results when doing search (3) and (4) too?
Many thanks.
Found the solution.
I need to wrap each word in the search phrase with
%string%.====
Search (1): “i love”
Query:
SELECT * from t WHERE name LIKE "%i% %love%";Results: “I love Rails”, “I love Django”
====
Search (2): “love”
Query:
SELECT * from t WHERE name LIKE "%love%";Results: “I love Rails”, “I love Django”
====
Search (3): “ails”
Query:
SELECT * from t WHERE name LIKE "%ails%";Results: “I love Rails”, “I code using Ruby on Rails”
====
Search (4): “code Ruby”
Query:
SELECT * from t WHERE name LIKE "%code% %Ruby%";Results: “I code using Ruby on Rails”
====
My Ruby code should be:
You can test this at http://sqlfiddle.com/#!2/d63f5/4