# Noob search question

**URL:** <https://www.ruby-forum.com/t/noob-search-question/73561>\
**Category:** Rails\
**Created:** [October 25, 2006, 10:12am UTC](https://www.ruby-forum.com/t/noob-search-question/73561 "2006-10-25T10:12:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Armitage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Armitage](https://www.ruby-forum.com/u/Armitage)\
**Post date:** [October 25, 2006, 10:12am UTC](https://www.ruby-forum.com/t/noob-search-question/73561/1 "2006-10-25T10:12:33Z")

</div>

Hi All,

I am beginning to think that mt brain doesnt speak Ruby(or rails)!

I am attempting to build the most basic search function imaginable -  
Search within the database for a particular location

Controller

@locations = Location.find(:all, :conditions =\> [“location like ?”,  
“%#{@search\_text}%”])

In my view I just put my basic scaffolded view from show, reasoing that  
this should return that exact result

View

## Find Results
\<% for column in Location.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @location.send(column.name) %\>

\<% end %\>

Help how do I resolve basic results like this? More properly I know it  
should return a table of all the close matches, but anything other than  
nasty NOMethoError# would be great!

---

<div class="post-metadata">

**Author:** ![Armitage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Armitage](https://www.ruby-forum.com/u/Armitage)\
**Post date:** [October 25, 2006, 11:39am UTC](https://www.ruby-forum.com/t/noob-search-question/73561/2 "2006-10-25T11:39:13Z")

</div>

First of, this find looks much cleaner and is more scure as everything  
is escaped by Rails:

@locations = Location.find(:all, :conditions =\> [“location like %?%”,  
@search\_text])

For the view: forget that scaffold stuff, it keeps you from learning  
how to do it yourself.  
in your current view, you loop through the Models _column headers_ and  
not the returned results in @location.  
\<%=h @location.send(column.name) %\> will therefore not return each  
results content, but throw an error i guess … not too fit in ruby  
myself, but been i while since touched scaffold for the reason  
mentioned above.

## Search Results

\<% @locations.each do |loc| %\>

**\<%= loc.location %\>**

\<%end%\>

…something like this, errors to be expected 😃

Note: i’m kind of new to Rails too so maybe my explanations where plain  
bullshit. Excuse me.

---

<div class="post-metadata">

**Author:** ![Armitage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Armitage](https://www.ruby-forum.com/u/Armitage)\
**Post date:** [October 25, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/noob-search-question/73561/3 "2006-10-25T14:31:17Z")

</div>

Thanks!

For whatever reason my last post didnt show up!

Here is where I am with this now.  
The ‘Location like %?%…’

Would result in a MySQL error like

Mysql::Error: You have an error in your SQL syntax; check the manual  
that corresponds to your MySQL server version for the right syntax to  
use near ‘%‘Hicom Room’%)’

So the wildcards are on the wrong side of the quotes

If I hard code  
@search\_text = ‘Hicom Room’ and take the wildcards out it works fine  
So… one step close I guess!

---

<div class="post-metadata">

**Author:** ![Armitage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@Armitage](https://www.ruby-forum.com/u/Armitage)\
**Post date:** [October 25, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/noob-search-question/73561/4 "2006-10-25T15:15:27Z")

</div>

OK

I done know if this is interesting to anyone esle, of if they can just  
shed some light on this.

I hasd been using a helper as the input for my search string:

\<%= start\_form\_tag :action =\> ‘find’ %\>  
\<%= text\_field :search, :search %\>  
\<%=submit\_tag ‘Find’ %\>

This turned out to be the cause of the Mysql error below as it seemed  
to be adding whitespace and new line characters,  
as soon as I changed the filed to be :

\<%= start\_form\_tag :action =\> ‘find’ %\>  
  
\<%=submit\_tag ‘Find’%\>

it worked straight away!

Any suggestions?
