为什么组计算字段不会显示在查询结果中?

我有这样的查询: query = Link.select('url, max(created_at) as created_at, count(*) as url_count').group(:url).order('url_count desc, created_at asc')

query.results.first示例结果:

 2.2.0 :002 > query.first => # 

为什么这里没有url_count ,即使我知道它是。

 2.2.0 :003 > query.first.url_count => 17 

计数一直存在,但模型to_s方法不知道它。

当控制台从query.first记录结果时使用的to_s方法在activerecord中的某处定义,它使用在数据库中为模型定义的属性。 由于您的属性仅针对此特定Link实例定义,而不是针对模型Link

我发现这非常有趣。 以下是如何构建控制台中显示的消息的说明。 它从gem active_attr开始,显示如下3种方法:

 def inspect attribute_descriptions = attributes.sort.map { |key, value| "#{key}: #{value.inspect}" }.join(", ") separator = " " unless attribute_descriptions.empty? "#<#{self.class.name}#{separator}#{attribute_descriptions}>" end # ... def attributes attributes_map { |name| send name } end # ... def attributes_map Hash[ self.class.attribute_names.map { |name| [name, yield(name)] } ] end 

方法attributes_names在gem activerecord定义

 def attribute_names @attribute_names ||= if !abstract_class? && table_exists? column_names else [] end end # ... def column_names @column_names ||= @connection.columns(@table_name).collect { |c| c.name } end 

这就是为什么你的计数没有出现的原因。

如果你真的希望它出现在你的控制台中,你可以覆盖inspect方法并将其添加到那里。