Shannon
Shannon

Reputation: 2744

Rails Acts as taggable on: Find related keywords on multiple taggable types

I'm creating a "Related content" area on a website and using the acts-as-taggable-on gem to do this. Then in an article, it pulls in related content by keywords matches. The following code works well with find_related_keywords (NOTE: Putting all the logic in the View to simplify and make this work for now, will refactor appropriately later)

<% for related in @article.find_related_keywords.limit(5) %>
    # Markup for related item in here       
<% end %>

However, this only returns related content in the same model type, in this case, Article. I also have other models/tables like Reviews, Interviews, etc that also have a :keywords taggable field defined and would like those included in the find_related_keywords results. Is there a way to do this?

Upvotes: 2

Views: 1247

Answers (2)

Ben Orozco
Ben Orozco

Reputation: 4381

Have you tried:

Review.tagged_with(@article.category_list, :any => true).limit(5)

Interview.tagged_with(@article.category_list, :any => true).limit(5)

Upvotes: 1

Shannon
Shannon

Reputation: 2744

After studying how Acts as Taggable On works - combined with the lack of response - I've concluded that it's not possible to do this and it's truly intended for just a single object type.

However, I have considered the alternative possibility of using search engines to my advantage. My project currently has Sunspot/Solr in it, I'm now thinking of using the keyword field to automatically search against the keywords, and in theory, that should work fine in returning the last five results or whatnot from any object / table that's indexed by Solr.

Upvotes: 0

Related Questions