Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
Build query:
SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc
結果:查詢,不能選擇列數
[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]
它不適合我回數列。我怎麼才能得到它?
當你只選擇'count(tags.id)'時它是否工作? –
是的,但是它只返回count列 –