# Acts\_as\_ferret; one search over multiple models?

**URL:** <https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724>\
**Category:** Rails\
**Created:** [May 9, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724 "2007-05-09T18:05:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdruse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@jdruse](https://www.ruby-forum.com/u/jdruse)\
**Post date:** [May 9, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724/1 "2007-05-09T18:05:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jdruse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@jdruse](https://www.ruby-forum.com/u/jdruse)\
**Post date:** [May 10, 2007, 12:52am UTC](https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724/2 "2007-05-10T00:52:07Z")

</div>

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](http://blog.ravet.com)

---

<div class="post-metadata">

**Author:** ![jdruse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@jdruse](https://www.ruby-forum.com/u/jdruse)\
**Post date:** [May 10, 2007, 12:59am UTC](https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724/3 "2007-05-10T00:59:07Z")

</div>

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](http://www.realitytechnicians.com)

---

<div class="post-metadata">

**Author:** ![jdruse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@jdruse](https://www.ruby-forum.com/u/jdruse)\
**Post date:** [May 10, 2007, 2:05am UTC](https://www.ruby-forum.com/t/acts-as-ferret-one-search-over-multiple-models/103724/4 "2007-05-10T02:05:11Z")

</div>

On May 9, 9:05 am, Jon D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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://rubyforge.org/pipermail/ferret-talk/2007-May/003275.html)  
[http://projects.jkraemer.net/acts\_as\_ferret/rdoc/classes/ActsAsFerret/ClassMethods.html#M000014](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/](http://infovore.org/archives/2006/09/22/cross-model-searching-in-rails-with-ferret/)
