Sticky:: Please read the post below. I think you will find the next method even simpler.
This is a short guide on adding search to your Rails models.
For this tutorial, I'm using a model called Product and product has the following attributes: name, description, price. If you're new to rails, what this means is that you have a database table called products, and this table has the columns name, description and price (including id of course).
Now, the first thing to do is to install the ferret gem. Without this, this tutorial is moot. Open a command prompt and type:
Last edited by daibatzu (2006-09-20 09:10:48)
Offline
Well I've found an even simpler way to implement search. This is unrelated to the iPod shuffle competition by the way (:-> hehe). Um here goes.
First you will need to download the search library by Duane Johnson and Nate McNamara. You can find it here:
Last edited by daibatzu (2006-10-06 04:43:11)
Offline
how did you get it off of the site? I et tons and tons of errors when using copy and paste from just checking that files syntax
Offline
Mmm, not sure what happened. It was OK previously. You can use the code below but it could be come outdated. The problem I think is in the quotes and double quotes used. Something like ”. Like they're using HTML character codes in Ruby code, maybe to make it look nicer or something? not sure.
Last edited by daibatzu (2006-10-06 04:55:50)
Offline
Ok I got an email asking how to build the search form for this. This is really quite simple and there a number of ways you can do it (including with AJAX). Anyway, here goes.
First, you'll need to add a search action to your controller. This could be like this:
Last edited by daibatzu (2006-10-24 12:38:10)
Offline
off hand would you know how to paginate this??
Offline
paginating is pretty simple. Somewhere on the rails wiki I found someone had written a paginate_collection method. Add it to application.rb
Offline
I should note that the above paginating does not work for searches, because when you go to page two the search query defaults to nothing and you lose all your search results. Haven't figured out a clean solution to that just yet.
Offline
Ok, I posted a tutorial on paginating search results (with semantically-meaningful labels, yet) at http://wiki.rubyonrails.com/rails/pages … teSearches Let me know what you think.
Last edited by paron (2007-01-24 13:08:57)
Offline
Great tutorial! How to search by category ? I mean if I'm developing my school library website, how I want to let the students search by by Category, Year, Author ? Like this forum search options. Thanks!
Offline
In the tutorial, the model provides two methods, "search_name" and "search_all."
For what you're trying to do, I would just provide one search method -- "search_author". I suspect search is overkill for the other two conditions, "category" and "year".
There's probably no need to "search" categories; you already know what they are. It's more of a filter, where you might pass an argument like ":conditions=>["category = 'fiction']. It's the same with year; you really don't need to 'search' it -- you're just going to filter for "1987" or ">1994 AND <2004". You're not going to search in the "year" field for terms like "Einstein" AND "math", so it isn't a search, per se.
Anyway, once your model returns its array of results, you could paginate and semantically label the pages with author data, so that the results are neatly broken into pages labeled "Aaron to Byron", "Camille to Dante", etc.
If it's a multi-year search, you could label them with "Able, 1883 to Anderson, 1994" and "Astarte, 1776 to Ballinger, 1923" or the other way around, like: "1810, Pursley to 1850, Bedinger" and "1851, Sydney to 1876, Lincoln" depending on your ordering of the results.
Is that what you're asking?
Ron
Last edited by paron (2007-01-24 12:47:43)
Offline
remove line numbers in vim: %s/^.\{,3}\d\{,3}\W//g
Offline
RoR newb here,
In suggested search.rhtml I found it didn't display the results. Eventually I found I had to change
Result: <% product.title %>
into
Result: <%= product.title %>
Offline
I hope this thread hasn't been dead and buried too long.
I am having trouble with the search.rb (the second version presented) file generating a syntax error. The problem is identified as being line 245:
r" is generating a syntax error - also appear on lines 293 and 297 of search.rb, respectively.
Last edited by kingworks (2008-03-19 14:46:43)
Offline
hi all i hv tried out this tutorial but getting an error
-->"undefined method `search' for #<Class:0x467cd08>"
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:1672:in `method_missing'
app/controllers/book_controller.rb:65:in `find'
Offline