Doing a simple search from the database?


#1

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?


#2

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.


#3

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 :slight_smile:


#4

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 :slight_smile:

…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.


#5

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

– G.


#6

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.