查询,无法选择列数 [英] query , can not select column count
本文介绍了查询,无法选择列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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">]
不返回计数列为了我。我怎么得到它?
It not return count column for me. How can I get it?
推荐答案
您是否尝试过调用 count
返回的 Tag
对象之一上的方法?仅仅因为检查
没有提到 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">]
仅包含 Tag
类知道的内容,而 Tag
仅包含有关内容的信息。 标签
表中的列:您只有 id
和 name
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.
如果您这样做:
tags = Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
,然后查看计数
s:
tags.map(&:count)
您将看到期望的计数数组。
You'll see the array of counts that you're expecting.
这篇关于查询,无法选择列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文