query , can not select column count

Have you tried calling the count method on one of the returned Tag objects? Just because inspect doesn’t mention the count doesn’t mean that it isn’t there. The inspect output:

[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]

will only include things that the Tag class knows about and Tag will only know about the columns in the tags table: you only have id and name in the table so that’s all you see.

If you do this:

tags = Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')

and then look at the counts:

tags.map(&:count)

You’ll see the array of counts that you’re expecting.

Leave a Comment