acts-as-taggable-on:找到名称为LIKE的标签,按tag_counts排序?

时间:2010-05-12 06:53:37

标签: sql ruby-on-rails ruby-on-rails-plugins acts-as-taggable-on

您好我正在使用rails插件acts-as-taggable-on,我正在尝试查找名称匹配且部分匹配给定查询的前5个最常用的标记。

当我做User.skill_counts.order('count DESC').limit(5).where('name LIKE ?', params[:query])

返回以下错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: name: SELECT     tags.*, COUNT(*) AS count FROM       "tags" INNER JOIN users ON users.id = taggings.taggable_id LEFT OUTER JOIN taggings ON tags.id = taggings.tag_id AND taggings.context = 'skills' WHERE     (taggings.taggable_type = 'User') AND (taggings.taggable_id IN(SELECT     users.id FROM       "users")) AND (name LIKE 'asd') GROUP BY  tags.id, tags.name HAVING    COUNT(*) > 0 ORDER BY  count DESC LIMIT 5

但是当我做User.skill_counts.first.name

返回

"alliteration"

我很感激这方面的任何帮助。

1 个答案:

答案 0 :(得分:4)

Tour Query应如下所示

SELECT  tags.*, COUNT(*) AS count FROM  "tags" 
         INNER JOIN users ON users.id = taggings.taggable_id 
         LEFT OUTER JOIN taggings ON tags.id = taggings.tag_id AND taggings.context = 'skills'
         WHERE  (taggings.taggable_type = 'User') AND 
                (taggings.taggable_id IN(SELECT users.id FROM "users")) AND 
               (tags.name LIKE 'asd') 
         GROUP BY  tags.id, tags.name HAVING COUNT(*) > 0 
         ORDER BY  count DESC 
         LIMIT 5

为此请尝试以下

User.skill_counts.order('count DESC').limit(5).where('tags.name LIKE ?', params[:query])