views:

147

answers:

1

Hi I'm using the rails plugin acts-as-taggable-onand 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 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

this returns

"alliteration"

I'd appreciate any help on this matter.

+1  A: 

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]) 
Salil