# Live search with one input field for few columns

**URL:** <https://www.ruby-forum.com/t/live-search-with-one-input-field-for-few-columns/133059>\
**Category:** Rails\
**Created:** [March 27, 2008, 10:04am UTC](https://www.ruby-forum.com/t/live-search-with-one-input-field-for-few-columns/133059 "2008-03-27T10:04:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSS\_awek\_Tuleja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@SSSSS\_awek\_Tuleja](https://www.ruby-forum.com/u/SSSSS_awek_Tuleja)\
**Post date:** [March 27, 2008, 10:04am UTC](https://www.ruby-forum.com/t/live-search-with-one-input-field-for-few-columns/133059/1 "2008-03-27T10:04:33Z")

</div>

Hello  
In Rails Cookbook is example of live search.  
There is one input field and we are looking for word (words) in TITLE  
column.

code:  
def self.search(terms)  
find :all,  
:conditions =\>  
[([“(LOWER(title) LIKE ?)”] \* terms.size ).join(" AND "), \*  
terms.flatten]  
end

# terms is an array with word (words)

My question is:  
How to change SEARCH function to search word (words) in more then one  
column eg. TITLE, BODY …  
Greate example of live search is here: [www.gotapi.com](http://www.gotapi.com)

Greetings

---

<div class="post-metadata">

**Author:** ![SSSSS\_awek\_Tuleja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@SSSSS\_awek\_Tuleja](https://www.ruby-forum.com/u/SSSSS_awek_Tuleja)\
**Post date:** [March 28, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/live-search-with-one-input-field-for-few-columns/133059/2 "2008-03-28T15:55:15Z")

</div>

This is so ugly, I can’t believe I’m posting it…

[“(” + ([“LOWER(title) LIKE ?”] \* terms.size).join(" AND “) + “)” + "  
OR " + “(” + ([“LOWER(body) LIKE ?”] \* terms.size).join(” AND ") +  
“)”, \* terms.flatten \* 2 ]

Be careful with the line breaks or it won’t work.

One problem with this (besides the field names being hard coded and it  
being a big, ugly mess) is that it will only find matches for one  
field. In other words, if a user entered “My favorite poem”, there  
would only be a match if all of those words were in the title OR if  
all of those words were in the body - it won’t find the “poem” if its  
title is “My Favorite Things” and the body contains “poem”.

Also, I assume that you have something like this in your code to put  
the wildcard characters in with your search terms:

terms.map {|t| “%#{t}%”}

If you’re doing more advanced searching, you might look into some  
plugins. I’ve heard many people talk about acts\_as\_ferret (with mixed  
reactions). Another option could be to allow the user to select what  
field he wants to search with a radio button list that corresponds to  
the possible search fields. You could then pass this value into a  
“field” variable for the search (i.e., “LOWER(#{field}) LIKE ?”…).

If you need something to work quickly, go ahead and try my solution.  
If you need something to work _well_, you might want to look  
elsewhere. 🙂

-Kyle
