logo       

Search Multiple Models: msg#00033

lang.ruby.ferret.general

Subject: Search Multiple Models

Hello folks,

I have four models, each with their own separate index.

Models = Articles, Blogs, MusicTracks, and MediaFiles

I have individual searches within each section of the site working just
fine, but I want to have a gloabl search that searches across all of
them at the same time and returns the results ordered by score.

Here's how far I am now...

def search
query = params[:query]
articles = Article.find_id_by_contents(query)
blogs = Blog.find_id_by_contents(query)
tracks = MusicTrack.find_id_by_contents(query)
files = MediaFile.find_id_by_contents(query)
matches = (articles + blogs + tracks + files).sort_by {|match|
match[:score] }
results = matches.collect {|match| :score => match[:score], :object
=> match[:model].find(match[:id])}
end

The block I'm passing isn't working and generates an exception at each
=>. Can anyone give me some advice on what I might be doing wrong or if
there is some better approach to doing this. It also seems that doing a
.find on each id in the model is a little inefficient, but I'm not sure
what else to try.

Any help suggestions would be greatly appreciated.

--
Posted via http://www.ruby-forum.com/.


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise