I have created a search box that queries a restaurant database. The restaurant model is set to search for "name LIKE ?". However, I cannot figure out how to add other conditions so that it can also display "address LIKE ?" and "city LIKE ?" For example, I want to type in the query "sushi" and find all of the restaurants that have the name sushi included in them. In this same box, I would also like to be able to type "oracle rd." and find all of the restaurants that have the phrase "oracle rd." in them. Essentially, I want to be able to do this in my restaurant model: def self.search(search) if search find(:all, :conditions => [''name OR address OR city LIKE ?'', "%# {search}%"]) else find(:all) end end However, when I do this, it only returns results from the city column. How can I get my find method to display results for all of these conditions?
Frederick Cheung
2009-Jun-19 09:00 UTC
Re: Search query: How to find results across columns
On Jun 18, 11:21 pm, evandrake <evancdr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > def self.search(search) > if search > find(:all, :conditions => [''name OR address OR city LIKE ?'', "%# > {search}%"]) > else > find(:all) > end > end > > However, when I do this, it only returns results from the city column.your conditions need to end up like "name like ''foo'' or address like ''foo'' or city like ''foo;" Fred> > How can I get my find method to display results for all of these > conditions?
Hi Can do like find(:all, :conditions => [''name LIKE ? OR address LIKE ? OR city LIKE ?'', "%# {search}%","%#{search}%","%#{search}%"]) Dont know this can be done any other way Sijo -- Posted via http://www.ruby-forum.com/.