views:

108

answers:

1

I'm using acts_as_taggable_on in a model, and am trying to implement the auto_complete plugin. It seems as though I have everything hooked up correctly, but the search isn't returning any results. Here's what I have so far:

In the view:

<%= text_field_with_auto_complete(:link, :tag_list, {}, {:tokens => ','}) %> 

In the controller:

  def auto_complete_for_link_tag_list 
    @tags = Link.tag_counts_on(:tags).where('tags.name LIKE ?', params[:link][:tag_list]) 
    render :inline => "<%= auto_complete_result(@tags, 'name') %>", :layout => false 
    logger.info "#{@tags.size} tags found."
  end 

The logger keeps returning 0 tags, and nothing shows up in the view (yeah, the layout includes the javascript defaults). The SQL that is being generated looks like:

SELECT tags.*, COUNT(*) AS count FROM "tags" LEFT OUTER JOIN taggings ON tags.id = taggings.tag_id AND taggings.context = 'tags' INNER JOIN links ON links.id = taggings.taggable_id WHERE (((tags.name LIKE 'so') AND (taggings.taggable_type = 'Link'))) GROUP BY tags.id, tags.name HAVING COUNT(*) > 0

Any thoughts or advice would be awesome.

+1  A: 

I ran into this problem and just ended up writing my own SQL, yours may look something like this:

@tags = Tag.find_by_sql("SELECT tags.name, tags.id
FROM tags JOIN taggings ON tags.id = taggings.tag_id 
JOIN links ON taggings.taggable_id = links.id 
JOIN groups ON links.group_id = groups.id 
WHERE groups.id = X
AND tags.name LIKE '%#{params[:link][:tag_list]}%'")

Youll need to update the groups.id = X portion, as I'm not sure how you are grabbing the group_id.

Tag is a class that the plugin provides, manually providing sql like this gives you all the flexibility in the world.

Rabbott