# :multi search (again)

**URL:** <https://www.ruby-forum.com/t/multi-search-again/150941>\
**Category:** Ferret\
**Created:** [October 15, 2008, 1:05am UTC](https://www.ruby-forum.com/t/multi-search-again/150941 "2008-10-15T01:05:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a6a055/32.png) [@davedave](https://www.ruby-forum.com/u/davedave)\
**Post date:** [October 15, 2008, 1:05am UTC](https://www.ruby-forum.com/t/multi-search-again/150941/1 "2008-10-15T01:05:04Z")

</div>

Hello

I’ve poured over as much of this topic regarding multi model searching  
in acts\_as\_ferret yet I’m still appearing to come up blank. Hopefully  
someone here could help,

I have the following in my artist model

artist model.rb

require ‘acts\_as\_ferret’  
class Artist \< ActiveRecord::Base  
acts\_as\_ferret :store\_class\_name =\> true, :fields =\> [:name,  
:sortname, :artist\_releases]

def artist\_releases  
return “#{self.release\_artist.release.name}”  
end

has\_many :release\_artists  
has\_and\_belongs\_to\_many :users, :join\_table =\> ‘user\_artists’  
has\_and\_belongs\_to\_many :tracks, :join\_table =\> ‘track\_artists’  
has\_and\_belongs\_to\_many :releases, :join\_table =\> ‘release\_artists’  
end

put simply, I’m trying to search and for a search for say “David Bowie”  
to return not only a David Bowie Artist record, but also Track and  
Release records by said artist.

Can any one help me figure this one out? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![davedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a6a055/32.png) [@davedave](https://www.ruby-forum.com/u/davedave)\
**Post date:** [October 16, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/multi-search-again/150941/2 "2008-10-16T20:50:43Z")

</div>

apologies for the poor form, but I’ve modified the below

> def artist\_releases  
> return “#{self.release\_artist.release.name}”  
> end

to say this

def artist\_releases  
self.releases  
end

this works, in as much as self.releases will return an array of releases  
by an artists, because of the HABTM relationship defined previously.  
However, I’m still confused as to how this then translates into the  
ability to search for “an artist” and to be returned related releases by  
said artist.

If anyone can help, or offer some pointers on this, I’d be quite  
appreciative.

thanks

---

<div class="post-metadata">

**Author:** ![davedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a6a055/32.png) [@davedave](https://www.ruby-forum.com/u/davedave)\
**Post date:** [October 28, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/multi-search-again/150941/3 "2008-10-28T20:10:41Z")

</div>

Hi!

First of all you should decide which exact behaviour you want - do you  
want your search results contain different model objects like tracks,  
releases and artists?

Or do you always want to find a certain type of model object, but also  
find it when somebody searches for a word contained in a property of  
some related record (like, say, find releases of an artist when  
searching for his name)?

Or a combination of both, so that searching for an artists name will  
return all his releases and the artist record itself?

First of all, every single model you expect in your result set has to  
call acts\_as\_ferret. Then, for every record, index all the information  
you think is useful, like you intend to do with the release names in  
your artist model. The artist\_releases method has to return a string  
containing the value you want to index - so something like

self.releases.map(&:name).join(’ ')

should work.

Watch the logs to see what values actually get indexed.

Cheers,  
Jens

Dave D. wrote:

> apologies for the poor form, but I’ve modified the below
> 
> > def artist\_releases  
> > return “#{self.release\_artist.release.name}”  
> > end
> 
> to say this
> 
> def artist\_releases  
> self.releases  
> end
> 
> this works, in as much as self.releases will return an array of releases  
> by an artists, because of the HABTM relationship defined previously.  
> However, I’m still confused as to how this then translates into the  
> ability to search for “an artist” and to be returned related releases by  
> said artist.
> 
> If anyone can help, or offer some pointers on this, I’d be quite  
> appreciative.
> 
> thanks

---

<div class="post-metadata">

**Author:** ![davedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a6a055/32.png) [@davedave](https://www.ruby-forum.com/u/davedave)\
**Post date:** [November 14, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/multi-search-again/150941/4 "2008-11-14T23:27:52Z")

</div>

Hi

How do I do the second one?

Say I have 2 x models (User and Address). I search both model’s  
properties i.e Kinman AND Leeds, I only want it to return only the User  
model in the results. It currently returns 2 x models (User and  
Address) in the results.

Thanks

Kinman

Jens KrÃ¤mer wrote:

> Hi!
> 
> First of all you should decide which exact behaviour you want - do you  
> want your search results contain different model objects like tracks,  
> releases and artists?
> 
> Or do you always want to find a certain type of model object, but also  
> find it when somebody searches for a word contained in a property of  
> some related record (like, say, find releases of an artist when  
> searching for his name)?
> 
> Or a combination of both, so that searching for an artists name will  
> return all his releases and the artist record itself?
> 
> First of all, every single model you expect in your result set has to  
> call acts\_as\_ferret. Then, for every record, index all the information  
> you think is useful, like you intend to do with the release names in  
> your artist model. The artist\_releases method has to return a string  
> containing the value you want to index - so something like
> 
> self.releases.map(&:name).join(’ ')
> 
> should work.
> 
> Watch the logs to see what values actually get indexed.
> 
> Cheers,  
> Jens
> 
> Dave D. wrote:
> 
> > apologies for the poor form, but I’ve modified the below
> > 
> > > def artist\_releases  
> > > return “#{self.release\_artist.release.name}”  
> > > end
> > 
> > to say this
> > 
> > def artist\_releases  
> > self.releases  
> > end
> > 
> > this works, in as much as self.releases will return an array of releases  
> > by an artists, because of the HABTM relationship defined previously.  
> > However, I’m still confused as to how this then translates into the  
> > ability to search for “an artist” and to be returned related releases by  
> > said artist.
> > 
> > If anyone can help, or offer some pointers on this, I’d be quite  
> > appreciative.
> > 
> > thanks
