# Search query: How to find results across columns

**URL:** <https://www.ruby-forum.com/t/search-query-how-to-find-results-across-columns/170581>\
**Category:** Rails\
**Created:** [June 19, 2009, 10:30am UTC](https://www.ruby-forum.com/t/search-query-how-to-find-results-across-columns/170581 "2009-06-19T10:30:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evandrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e9c0ed/32.png) [@evandrake](https://www.ruby-forum.com/u/evandrake)\
**Post date:** [June 19, 2009, 10:30am UTC](https://www.ruby-forum.com/t/search-query-how-to-find-results-across-columns/170581/1 "2009-06-19T10:30:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![evandrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e9c0ed/32.png) [@evandrake](https://www.ruby-forum.com/u/evandrake)\
**Post date:** [June 19, 2009, 11:01am UTC](https://www.ruby-forum.com/t/search-query-how-to-find-results-across-columns/170581/2 "2009-06-19T11:01:28Z")

</div>

On Jun 18, 11:21 pm, evandrake [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![evandrake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e9c0ed/32.png) [@evandrake](https://www.ruby-forum.com/u/evandrake)\
**Post date:** [June 19, 2009, 11:17am UTC](https://www.ruby-forum.com/t/search-query-how-to-find-results-across-columns/170581/3 "2009-06-19T11:17:48Z")

</div>

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
