Forum: Ruby on Rails Simple Search

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam B. (Guest)
on 2006-04-10 03:01
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
David H. (Guest)
on 2006-04-10 03:25
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' %>
David H. (Guest)
on 2006-04-10 03:33
Ooops...accidently hit 'Enter' and posted...

to continue...

<%= start_form_tag :action => 'find_category_by_name' %>
  Find Category:<br/>
  <input type="text" 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/

Good luck...
David H. (Guest)
on 2006-04-10 03:34
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
Michael J. (Guest)
on 2006-04-10 03:39
(Received via mailing list)
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/Bas...
Adam B. (Guest)
on 2006-04-10 04:42
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
Jean-François (Guest)
on 2006-04-10 04:54
(Received via mailing list)
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.
Michael J. (Guest)
on 2006-04-10 21:32
(Received via mailing list)
The link I posted
(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.
This topic is locked and can not be replied to.