Search code examples
ruby-on-railsruby-on-rails-3jquery-autocompleteacts-as-taggable-on

acts_as_taggable_on and rails autocomplete gem - displaying tag counts


I am using the acts_as_taggable_on gem and the rails jquery autocomplete gem

I know how to get the tag counts via the gem

I know that in order to display something else other than the return search results for the autocomplete I need to do something like this

autocomplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag', :full => true

and adding 2 more keys which are

:extra_data , :display_value

the extra data will retrieve the more data in the search (not sure it is needed here) the display_value will call a method from within the model which is searched with the autocomplete.

The problem:

I don't know where to put the method for the display_value which should be something like

def tags_with_counts
 "#{tag.name} x #{tag.count}
end

As I don't have a tag model file to put it in (since the acts_as_taggable_on does not generate such file.

But even if I did have such file, from previous experiments, that method can only use data that is retrieved with the autocomplete search, and I don't have a column with tag counts.

What do I do to solve this?


Solution

  • solved it by doing

    class Tag < ActsAsTaggableOn::Tag
    
    
      def tagcount
           num = ActsAsTaggableOn::Tagging.where(:tag_id => self.id).count
           "#{Tag.find(self.id).name} x #{num}"
      end
    
    
    end
    

    and sending the "Tag" class to the rails-autocomplete call