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
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’ %>
Ooops…accidently hit ‘Enter’ and posted…
to continue…
<%= start_form_tag :action => ‘find_category_by_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…
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
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/Base.html#M000344
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.
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.
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