Rails metasearch +acts-as-taggable-on 多标签查询求助~

Jacob · 2012年10月20日 · 2146 次阅读

Model:

# encoding: utf-8
class Product < ActiveRecord::Base
  acts_as_taggable
  acts_as_taggable_on :skills, :humans
end

HTML:

 <%= form_for @search, :url =>  search_path do |f| %>
     <%= f.text_field  :skill_taggings_tag_name_eq %>
<%end%>

生成的SQL:

SELECT `products`.* FROM `products` 

LEFT OUTER JOIN `taggings` 
ON `taggings`.`taggable_id` = `products`.`id` AND `taggings`.`taggable_type` = 'Product' AND taggings.context = 'skills' 
LEFT OUTER JOIN `tags` 
ON `tags`.`id` = `taggings`.`tag_id` 

WHERE `products`.`published` = 1 AND `products`.`category_id` = 4 
AND `tags`.`name` = '老年人'

这样没有任何问题,但是如果有2个以上标签联合查询的时候,就查不到一条数据: HTML:

 <%= form_for @search, :url =>  search_path do |f| %>
     <%= f.text_field  :skill_taggings_tag_name_eq %>
     <%= f.text_field  :human_taggings_tag_name_eq %>
<%end%>

生成的SQL:

SELECT `products`.* FROM `products` 

LEFT OUTER JOIN `taggings` 
ON `taggings`.`taggable_id` = `products`.`id` AND `taggings`.`taggable_type` = 'Product' 
AND taggings.context = 'skills' #这是taggings.context
LEFT OUTER JOIN `tags` 
ON `tags`.`id` = `taggings`.`tag_id` 

LEFT OUTER JOIN `taggings` `humans_taggings_products` 
ON `humans_taggings_products`.`taggable_id` = `products`.`id` AND `humans_taggings_products`.`taggable_type` = 'Product' 
AND taggings.context = 'humans' #这里也是taggins.context
LEFT OUTER JOIN `tags` `tags_taggings` 
ON `tags_taggings`.`id` = `humans_taggings_products`.`tag_id` 

WHERE `products`.`published` = 1 AND `products`.`category_id` = 4 
AND `tags`.`name` = '老年人' AND `tags_taggings`.`name` = '增强免疫' 

原因是2个左链接使用了相同的taggings.context, 如果把第二个taggings.context='humans'改成humans_taggings_products='humans'就OK了

github上有相同的问题https://github.com/ernie/meta_search/issues/104

不知道大家有没有办法或者解决思路,谢谢!

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册