[Ferret-talk] Multiple Model Search

Jens Kraemer kraemer at webit.de
Thu Apr 12 09:02:13 EDT 2007


[from out-of-band communication with steven, just for the record]

Hi!

On Wed, Apr 11, 2007 at 01:47:49PM +0200, Steven Garcia wrote:
> Hey Jens,
> 
[..]
> 
> 1. Is the Search model just an Active Record object or do I need to  
> set up a database for it as well (if so then with what columns)?

No, that search model is just a plain class that backs your search form.
If you want to take advantage of AR's validations for the search form,
you can use the active_record_base_without_table rails plugin that gives
you a base class for your model similar to ActiveRecord::Base, but
without the need for a db table :-)

> 2. Do I need to set up table relationships to the other models
> (has_many) or do my models just inherit from the search class?

No and no :-) A sample search model might look like this:

class Search
  attr_accessor :query

  def do_search
    return Model.find_by_contents(query)
  end
end

any query preprocessing or extended search form fields would go in
there, too. In your controller's search action you then only need to do 

search = Search.new(params[:search])
@results = search.do_search


> 3. Can I still boost certain fields within various models?

As the boost for fields is set in your call to acts_as_ferret in the
specific model you want to index, there's no problem setting this in
various models.

Btw, if you want to search across multiple Model classes at once, use
the multi_search method instead of find_by_contents.


Jens

-- 
Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
kraemer at webit.de | www.webit.de
 
Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


More information about the Ferret-talk mailing list