# Searching a database

**URL:** <https://www.ruby-forum.com/t/searching-a-database/116256>\
**Category:** Rails\
**Created:** [October 3, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/searching-a-database/116256 "2007-10-03T22:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 3, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/searching-a-database/116256/1 "2007-10-03T22:30:00Z")

</div>

I realize this is an SQL problem but figured a lot of people using ROR  
have came across this before. I pulled apart the string that was  
entered into the search form and stored it in textArray. The problem I  
am having is when I include commmon words such as the, near, view and so  
on into the search field I end up with zero results. when I only search  
for less common words such as rocky and argentina for example the search  
works fine. at first I thought that maybe it was generating too many  
results and was screwing up but when i search for rocky it works and  
when i search for near rocky it doesnt.

I realize the code might not be the most efficient but here it is…

@searches\_pages, @searches = paginate(:searches, :per\_page =\> 10,  
:conditions =\> getSearch(textArray,params[:country]))

def getSearch(textArray,selectedCountry )  
result = []  
string = “”  
if selectedCountry != “Optional Field”  
string \<\< "country = (?) and "  
end  
textArray.each do |x|  
if textArray[textArray.length - 1] == x  
string \<\< "match(country,caption, keywords, notes) against  
(?) "  
else  
string \<\< "match(country, caption, keywords, notes) against  
(?) and "  
end  
end  
result \<\< string  
if selectedCountry != “Optional Field”  
result \<\< selectedCountry  
end  
textArray.each do |x|  
result \<\< x  
end  
result  
end

Im not sure if i supplied enough information but I am trying to finish  
this project soon so any type of responses would help. Also, if there  
is an easier way to do an sql search based off of what is entered into  
the search field please let me know. The reason I did this is because I  
wasn’t sure how many words the user would be entering into the field.  
And without knowing this I could not hard code the conditions =\> so I  
wrote a helper method.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 4, 2007, 6:28am UTC](https://www.ruby-forum.com/t/searching-a-database/116256/2 "2007-10-04T06:28:04Z")

</div>

Chris H. wrote:

> I realize this is an SQL problem but figured a lot of people using ROR  
> have came across this before. I pulled apart the string that was  
> entered into the search form and stored it in textArray. The problem I  
> am having is when I include commmon words such as the, near, view and so  
> on into the search field I end up with zero results. when I only search  
> for less common words such as rocky and argentina for example the search  
> works fine. at first I thought that maybe it was generating too many  
> results and was screwing up but when i search for rocky it works and  
> when i search for near rocky it doesnt.

You might want to look into using Ferret.

[http://projects.jkraemer.net/acts\_as\_ferret/](http://projects.jkraemer.net/acts_as_ferret/)

–  
Michael W.
