# Making a Keyword Search

**URL:** <https://www.ruby-forum.com/t/making-a-keyword-search/170584>\
**Category:** Rails\
**Created:** [June 19, 2009, 11:19am UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584 "2009-06-19T11:19:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adsouzp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@adsouzp](https://www.ruby-forum.com/u/adsouzp)\
**Post date:** [June 19, 2009, 11:19am UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584/1 "2009-06-19T11:19:36Z")

</div>

Hello I created a search engine on my site but it only works when you  
type the full name of the string. My string name are on average 9  
characters long.

I would like to be able to search one word of the string and then get  
the listings to show up that have what you searched in it. This is what  
I have:

* * *

home.html.erb

\<% form\_tag “/home/find”, :method =\> ‘get’ do %\>

\<%= text\_field\_tag :search, params[:search] %\>  
\<%= submit\_tag “Search”, :name =\> nil %\>

\<% end %\>

* * *

home\_controller

def find  
@deals = Deal.find(:all,  
:conditions =\> [“item\_title = ? OR description = ?”,  
params[:search], params[:search]])  
end

---

<div class="post-metadata">

**Author:** ![adsouzp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@adsouzp](https://www.ruby-forum.com/u/adsouzp)\
**Post date:** [June 19, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584/2 "2009-06-19T12:05:58Z")

</div>

Aaron D. wrote:

> Hello I created a search engine on my site but it only works when you  
> type the full name of the string. My string name are on average 9  
> characters long.
> 
> I would like to be able to search one word of the string and then get  
> the listings to show up that have what you searched in it. This is what  
> I have:
> 
> * * *
> 
> home.html.erb
> 
> \<% form\_tag “/home/find”, :method =\> ‘get’ do %\>
> 
> \<%= text\_field\_tag :search, params[:search] %\>  
> \<%= submit\_tag “Search”, :name =\> nil %\>
> 
> \<% end %\>
> 
> * * *
> 
> home\_controller
> 
> def find  
> @deals = Deal.find(:all,  
> :conditions =\> [“item\_title = ? OR description = ?”,  
> params[:search], params[:search]])  
> end

Hey - check out [MySQL Tutorial - Where](http://www.tizag.com/mysqlTutorial/mysqlwhere.php)

You can use wildcards in your mysql queries.

An example in rails would be:

Deal.find(:all, :conditions =\> [“item\_title LIKE ?”,  
“%#{params[:search]}%”])

Also - why have you set the value for the text\_field\_tag to  
params[:search]?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![adsouzp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@adsouzp](https://www.ruby-forum.com/u/adsouzp)\
**Post date:** [June 19, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584/3 "2009-06-19T18:45:42Z")

</div>

> Deal.find(:all, :conditions =\> [“item\_title LIKE ?”,  
> “%#{params[:search]}%”])
> 
> Also - why have you set the value for the text\_field\_tag to  
> params[:search]?
> 
> Gavin
> 
> [http://handyrailstips.com](http://handyrailstips.com)

The params[:search] was from:

> **[\#37 Simple Search Form - RailsCasts](http://railscasts.com/episodes/37-simple-search-form)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

I tried to go by that at first. And a book I have told me to use  
params[:search\_string]. I also tried messing with the item LIKE ? the  
other day but didn’t get to far but I’ll try messing with it again.

I’ll post again after I checked your link and try a few other things.  
Thanks

---

<div class="post-metadata">

**Author:** ![adsouzp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@adsouzp](https://www.ruby-forum.com/u/adsouzp)\
**Post date:** [June 19, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584/4 "2009-06-19T18:57:56Z")

</div>

You’re welcome 🙂

I see what you’ve done with the params[:search]

This is used to show the search term in the text-box on the results  
page.

This seems perfectly reasonable

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

Aaron D. wrote:

> I alright I think my problem was that I was using to sources of  
> different information.
> 
> I used:  
> :conditions =\> [“item\_title LIKE ?”, “%#{params[:search]}%”])  
> and it worked.
> 
> Thanks Gavin for the help!

---

<div class="post-metadata">

**Author:** ![adsouzp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@adsouzp](https://www.ruby-forum.com/u/adsouzp)\
**Post date:** [June 19, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/making-a-keyword-search/170584/5 "2009-06-19T18:52:17Z")

</div>

I alright I think my problem was that I was using to sources of  
different information.

I used:  
:conditions =\> [“item\_title LIKE ?”, “%#{params[:search]}%”])  
and it worked.

Thanks Gavin for the help!
