Search code examples
ruby-on-railssearchsearch-engine

How to handle related objects by attribute?


I have a Ruby on Rails model that has quite a few HABTM relationships. Essentially, i'm making a video sharing site themed around skateboarding for a client. Each video has many attirbutes through HABTM: Location, Skateboarders, tags, location, music, equipment, spot, region, etc... And it also has a few class attributes that are significant: Title, description, etc...

The idea here is my client would like a "Related Videos" feed by each video player, using all of the formerly mentioned descriptive data. Furthermore, the information should be weighted (similar titles take precedence over similar tags).

I was trying to find a good way implement Google SiteSearch to handle the grunt work, but can't find a good search term syntax ie:

inurl: example.com/videos related:example.com/videos/4638872 

Which doesn't actually work, unfortunately...


Solution

  • Solved this by using ferret to do a keyword explode search.

    def related
        string = self.name + " " + self.tag_name + " " + self.skateboarder_name + " " + self.category_name + " " + self.editor_name
        #probably could have used "join" there...
        s = "*" + string.gsub(" ", "* OR *") + "*"
        relates = Video.find_with_ferret(s)
        #takes out duplicates and gices 10 to choose from
        while relates.count <= 10
            relates << Video.random
            relates = relates.uniq
        end
        #take out original video
        relates = relates - [Video.find(self.id)]
        #send back with "middle data"... for some reason i thought this might be more accurate....
        return relates[1..8]
    end