Anand
Anand

Reputation: 3770

How do I make ActsAsTaggableOn::Tag to be 'acts_as_indexed'?

I have a rails 3 app where I am using acts_as_taggable_on to tag a Post model. I have a search box which allows me to search through the posts, where I use acts_as_indexed. In my search, I want to also be able to search through all the tags, by extending the ActsAsTaggableOn::Tag class to support acts_as_indexed. How do I do that?

I tried the following:

# lib/tag.rb
module ActsAsTaggableOn
 class Tag
   acts_as_indexed :fields => [:name]
 end
end

but then I get the following error when I try to call find_with_index:

> ActsAsTaggableOn::Tag.find_with_index('foo')
undefined method `find_with_index' for ActsAsTaggableOn::Tag(id: integer, name: string):Class

UPDATE: I made this work using acts_as_taggable_on's built in finders (specifically, Tag#named_like), but I would prefer to use acts_as_indexed, since I use it with all the other models in my app, and I am having to special case tag searching due to this issue.

Thanks,

Upvotes: 0

Views: 350

Answers (1)

ReggieB
ReggieB

Reputation: 8257

It might be easier to extend the act_as_indexed declaration on the Post model so that tags related to each post are included in a search.

class Post
  acts_as_taggable_on
  acts_as_indexed :fields => [ <existing tag list> , :tag_list]

Upvotes: 1

Related Questions