# Doing a simple search from the database?

**URL:** <https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176>\
**Category:** Rails\
**Created:** [June 12, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176 "2006-06-12T23:35:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 12, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/1 "2006-06-12T23:35:12Z")

</div>

I realize thi may be more complex issue than can be asnwered in this  
post. But, what I need to do is create a simple query on my existing  
tables via a search form. I have half my app done (I can write all the  
info I want to my database), but pulling the data out dynamically is  
giving me trouble.

I have both the “ruby for rails” book and the Agile Development book.  
They both give great info on static queries. Such as:

post = Posts.find\_all\_by\_title(“green”)

But I need to be able to pull the record dynamically. For instance, a  
form where I can type in “green” and search the “title” and “body”  
columns in the “posts” table.

It seems like a simple enough query but I’m having trouble finding good  
documentation. Any ideas on how to do this or where I might find a good  
tutorial for this kind of query?

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 12, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/2 "2006-06-12T23:55:38Z")

</div>

Jasbur wrote:

> I realize thi may be more complex issue than can be asnwered in this  
> post. But, what I need to do is create a simple query on my existing  
> tables via a search form. I have half my app done (I can write all the  
> info I want to my database), but pulling the data out dynamically is  
> giving me trouble.
> 
> I have both the “ruby for rails” book and the Agile Development book.  
> They both give great info on static queries. Such as:
> 
> post = Posts.find\_all\_by\_title(“green”)
> 
> But I need to be able to pull the record dynamically. For instance, a  
> form where I can type in “green” and search the “title” and “body”  
> columns in the “posts” table.
> 
> It seems like a simple enough query but I’m having trouble finding good  
> documentation. Any ideas on how to do this or where I might find a good  
> tutorial for this kind of query?

In your action code:

post = Post.find\_all\_by\_title(params[:title])

Your form should have a field like:

If you want other fields involved in the search, you can use  
find\_all\_by\_field\_1\_and\_field\_2 I believe. More than that, and you could  
do seperate finds and then intersect them. I’m not sure what the best  
practice is for situations like that.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 13, 2006, 12:21am UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/3 "2006-06-13T00:21:28Z")

</div>

Ok, that helped. But now the form seems to take the input. But when I  
hit the “search” button it just stays on the search page and clears out  
the text area. I’ll post my code below.

search.rhtml:

\<%= start\_form\_tag :action=\> “search” %\>

### Search
 \<%= error\_messages\_for 'post' %\>  

```
    <input type=text name="title">
    <input type="submit" value="Search &#187;" class="primary" />

<%= end_form_tag %>

```

post\_controller.rb:

def search  
post = Post.find\_all\_by\_title(params[:title])  
end

I realize I’m probablly missing something in the “search” action to  
display the results. But this is where I’m hitting a brick wall. I’m  
having trouble cramming one more peice of information into my head 🙂

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 13, 2006, 12:25am UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/4 "2006-06-13T00:25:11Z")

</div>

> I realize I’m probablly missing something in the “search” action to  
> display the results. But this is where I’m hitting a brick wall. I’m  
> having trouble cramming one more peice of information into my head 🙂

…yeah, that about sums it up. Add something like

\<%= post.value %\>

In the body somewhere to display the results. You’ll probably get an  
error if you go to the page with no search text, so check to make sure  
post isn’t nil before attempting to print anything about it.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 13, 2006, 12:35am UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/5 "2006-06-13T00:35:34Z")

</div>

I think in your search method post should really be @post …

– G.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [October 4, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/doing-a-simple-search-from-the-database/63176/6 "2006-10-04T22:55:53Z")

</div>

I’m looking at creating the same search he did, and I even used his code  
as a starting point (thanks!). However, I still can’t get this to work.  
It takes my search query and spits out a page that says " NoMethodError  
in Recipes#results" and “undefined method `value’ for []:Array”. Here’s  
what I have so far:

search.rhtml:

\<%= start\_form\_tag :action=\> “search” %\>

### Search

\<%= end\_form\_tag %\>

results.rhtml:

\<%= @recipe.value %\>

recipes\_controller.rb:

def search  
@recipe = Recipe.find\_all\_by\_title(params[:title])  
redirect\_to :action =\> ‘results’  
end

def results  
@recipe = Recipe.find\_all\_by\_title(params[:title])  
end

Thanks!

Dave

Guido S. wrote:

> I think in your search method post should really be @post …
> 
> – G.
