开发者

acts_as_taggable_on and auto_complete returning no results

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_res开发者_如何学Cult(@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.


You can also build a similar query using ActiveRecord

def self.tagged_like(term)
  conditions = ["taggings.taggable_type = 'Link' AND LOWER(tags.name) LIKE ?",
                "%#{term.downcase}%"]

   ActsAsTaggableOn::Tag.find(:all,  
                              :include => "taggings", 
                              :conditions => conditions)
end

This generates the following SQL:

SELECT [..fields..] FROM "tags" LEFT OUTER JOIN "taggings" ON taggings.tag_id = tags.id 
WHERE (taggings.taggable_type = 'Link' AND LOWER(tags.name) LIKE '%something%')

AATO version 2.0.6


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜