acts-as-taggable-on: find tags with name LIKE, sort by tag_counts?
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.
When I do User.skill_counts.order('count DESC').limit(5).where('name LIKE ?', params[:query])
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 OU开发者_运维技巧TER 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
this returns
"alliteration"
I'd appreciate any help on this matter.
Tour Query should look like this
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])
精彩评论