:multi search (again)


#1

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.


#2

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


#3

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


#4

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