Hi I'm using the rails plugin acts-as-taggable-on
and I'm trying to find the top 5 most used tags whose names match and partially match a given query.
你好,我正在使用rails插件acts-as-taggable- on并且我正在尝试找到最常用的5个标签,它们的名字匹配并且部分匹配给定的查询。
When I do User.skill_counts.order('count DESC').limit(5).where('name LIKE ?', params[:query])
当我做User.skill_counts。订单(计数DESC).limit(5)。(“名字?”,params[:查询])
This return the following error:
这将返回以下错误:
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
But when I do User.skill_counts.first.name
但是当我使用user。skill_counts.first.name时
this returns
这将返回
"alliteration"
I'd appreciate any help on this matter.
在这件事上我希望得到任何帮助。
1 个解决方案
#1
4
Tour Query should look like this
Tour查询应该如下所示
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
For this Try following
这个尝试后
User.skill_counts.order('count DESC').limit(5).where('tags.name LIKE ?', params[:query])
#1
4
Tour Query should look like this
Tour查询应该如下所示
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
For this Try following
这个尝试后
User.skill_counts.order('count DESC').limit(5).where('tags.name LIKE ?', params[:query])