I have the following in my model to search for a string entered by a user:
def self.find_products(product="")
find(:all, :select => 'product_id, name', :order => "name", :conditions => ["name like ? and locale =?", "%#{product.capitalize}%", I18n.locale])
end
But it only works if the string is one word.
If the string contains two or more words such as “card guides” it returns nothing even if those are the exact words in the name.
How can I do this search properly.
Rails 3 Ruby 1.9.2
Update
It turns out that its a Postgresql syntax. I needed to user iLIKE for case insensitivity. Then it worked better. I still like the answer below as it also helped to refine my syntax.
Hey i am also using this type of search logic in mine project, I had done in this way . If you want to try then look if it might helps you -:
Thanks