# Simple Search

**URL:** <https://www.ruby-forum.com/t/simple-search/93783>\
**Category:** Rails\
**Created:** [April 10, 2006, 1:01am UTC](https://www.ruby-forum.com/t/simple-search/93783 "2006-04-10T01:01:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 1:01am UTC](https://www.ruby-forum.com/t/simple-search/93783/1 "2006-04-10T01:01:00Z")

</div>

I’m trying to implement a search system and I’m lost as to how to go  
about it.

I’m searching the model “category”, and I’m searching the “name” field.  
From there I’ll use @categories.listings to display. But that’s the easy  
part.

I assume I need to use a LIKE command in MySQL, but I don’t know how to  
do that from Rails.

If anyone can give me any ideas on where to look or how to get started  
I’ll greatly appreciate it!

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 1:25am UTC](https://www.ruby-forum.com/t/simple-search/93783/2 "2006-04-10T01:25:22Z")

</div>

Adam,

I am not sure how sophisticated you want your search to be, but a simple  
way to do it would be to put this in the view corresponding to your  
category controller:

\<%= start\_form\_tag :action =\> ‘find\_category\_by\_name’ %\>

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 1:33am UTC](https://www.ruby-forum.com/t/simple-search/93783/3 "2006-04-10T01:33:03Z")

</div>

Ooops…accidently hit ‘Enter’ and posted…

to continue…

\<%= start\_form\_tag :action =\> ‘find\_category\_by\_name’ %\>  
Find Category:  
  
  
\<%= submit\_tag ‘Search’ %\>  
\<%= end\_form\_tag %\>

In your controller:

def find\_category\_by\_name  
@categories = Category.find(:all, :conditions =\> [‘name = ?’,  
params[:find\_category]]  
redirect\_to :action =\> ‘list\_categories’

def find\_categories

end

You would then create a view for the ‘find\_categories’ action that  
listed your categories. Of course, you could use a drop-down box that  
listed all of your category names for the search field. To get even more  
fun out of it, try the Auto-Complete field magic from  
[http://script.aculo.us/](http://script.aculo.us/)

Good luck…

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 1:34am UTC](https://www.ruby-forum.com/t/simple-search/93783/4 "2006-04-10T01:34:37Z")

</div>

Wow…long night I guess…the last method should read:

def list\_categories

end

NOT def find\_categories…

Even screwed up my own example code. Have a nice day!

-David

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 1:39am UTC](https://www.ruby-forum.com/t/simple-search/93783/5 "2006-04-10T01:39:04Z")

</div>

This is so easy even a total noob like me can answer it 🙂

What you are looking for is the ActiveRecord find() method. Something  
like: @categories = Category.find(:all, :conditions =\> [“name = ?”,  
some\_variable])

Here’s the syntax:  
[http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000344](http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000344)

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 2:54am UTC](https://www.ruby-forum.com/t/simple-search/93783/6 "2006-04-10T02:54:56Z")

</div>

Hello Adam,

> Those methods are a good start, but there’s a problem. 🙂 I have a lot  
> of categories, which is why I want a search for them. I need the search  
> to match similar names as well as exact; so searching for “insurance”  
> finds “investment and insurance.”
> 
> That’s why I mentioned I thought it would involve MySQLs “LIKE”  
> command. I realize I could write a find\_by\_sql instruction that would  
> probably work, but if there’s a cleaner more Rails-y way to do it I’d like  
> to try that.

Try :

@categories = Category.find :all,  
:conditions =\> [‘name LIKE ?’, “%#{params[:find\_category]}%” ]

Good night,

```
  -- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/simple-search/93783/7 "2006-04-10T19:32:26Z")

</div>

The link I posted  
([http://ar.rubyonrails.org/classes/ActiveRecord/Base.html](http://ar.rubyonrails.org/classes/ActiveRecord/Base.html)) explained  
this: “Conditions can either be specified as a string or an array  
representing the WHERE-part of an SQL statement.” Sorry if you missed  
that bit–should have pointed it out.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 10, 2006, 2:42am UTC](https://www.ruby-forum.com/t/simple-search/93783/8 "2006-04-10T02:42:22Z")

</div>

Those methods are a good start, but there’s a problem. 🙂 I have a lot  
of categories, which is why I want a search for them. I need the search  
to match similar names as well as exact; so searching for “insurance”  
finds “investment and insurance.”

That’s why I mentioned I thought it would involve MySQLs “LIKE” command.  
I realize I could write a find\_by\_sql instruction that would probably  
work, but if there’s a cleaner more Rails-y way to do it I’d like to try  
that.

-Adam
