0

I have two records in keyskill tables name "SAP FICO" and "FICO SAP". When I search the record with "FICO SAP". I want to get both records. Is there any way to fetch this record if I search?

I want this both as result if I search, I tried using like as below, but it fetches only one.

    search = "FICO SAP"
    KeySkill.where("name LIKE ?", "%#{search}%")

This is my Keyskill table data (created like this in table)

 <KeySkill id: 50, name: "SAP FICO", relevant_experience: "1", created_at: "2019-01-06 08:48:54", updated_at: "2019-01-06 08:48:54", deleted_at: nil> 
<KeySkill id: 51, name: "FICO SAP", relevant_experience: "1", created_at: "2019-01-06 08:48:55", updated_at: "2019-01-06 08:48:55", deleted_at: nil> 
1
  • What database are you using? There are some full-text search gems out there that are pretty simple to use. Commented Jan 7, 2019 at 19:31

1 Answer 1

1

A simple way to handle this would be something like

search_term = "FICO SAP" 
terms = search_term.split 

filtered_skills = KeySkill 
terms.each do |t| 
  filtered_skills = filtered_skills.where("name like '%#{term}%'") 
end 

and then just use the filtered_skills to retrieve them.

This works where each part of the original search term has to be present in a random order. So we can just use and for all terms.

[EDIT: if you want to search case insensitive]

If you want this search to be case independent, you can rewrite the where as follows if you are using postgresql

.where("name ilike '%#{term}%'") 

(ilike = case Insensitive LIKE). But this method only exists for postgresql, so for any other database you would use the following:

.where("lower(name) like '%#{term.downcase}%'")     
Sign up to request clarification or add additional context in comments.

2 Comments

Better use ILIKE if you're using PostgreSQL
Good point! I extended my answer to show how to do case insensitive searching, if the OP would find that useful (actually always seems smart?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.