Acts_as_ferret; one search over multiple models?

So i’ve been reading acts_as_ferret for about 3 days now and still have
not figured out how to create one search over all my models. that way
when people search my site, they can actually find what they’re looking
for, and not just one model at a time.

Jon,

not figured out how to create one search over all my models.

It’s not that hard to write :
results = Book.find_by_contents(query) + Disk.find_by_contents(query)

If you really have loads of models to search through, you could write
something like (untested) :

MODELS=%w(Book Disk Person Animal Article Page Concert Joke Story)
results = MODELS.collect{|klass|
klass.constantize.find_by_contents(query)
}.flatten

Alain

blog.ravet.com

You could also always make a model that covers them all through
methods, using the ability to point acts as ferret at other fields.

Example:

class Order < ActiveRecord::Base # ( or not even maybe… )
belongs_to :reservation
belongs_to :customer
acts_as_ferret :remote => true, :additional_fields => [
:reservation_to_s, :customer_to_s ]

def reservation_to_s
self.reservation.to_s # assuming you defined it
end
def customer_to_s
self.customer.to_s # ditto
end
end

Just one option… of many.

D. Taylor S.,
Reality Technicians
http://www.realitytechnicians.com

On May 9, 9:05 am, Jon D. [email protected] wrote:

So i’ve been reading acts_as_ferret for about 3 days now and still have
not figured out how to create one search over all my models. that way
when people search my site, they can actually find what they’re looking
for, and not just one model at a time.


Posted viahttp://www.ruby-forum.com/.

Look at multisearch
http://rubyforge.org/pipermail/ferret-talk/2007-May/003275.html
http://projects.jkraemer.net/acts_as_ferret/rdoc/classes/ActsAsFerret/ClassMethods.html#M000014

or combine searches over individual models
http://infovore.org/archives/2006/09/22/cross-model-searching-in-rails-with-ferret/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs