为什么组计算字段不显示在查询结果中? [英] Why group calculation fields do not show up in query result?

查看:84
本文介绍了为什么组计算字段不显示在查询结果中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询:
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 
=> #<链接ID:无,URL: http:// 1,created_at: 2015-03-10 16:43:54>

为什么这里甚至没有 url_count

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


解决方案

除了模型 to_s 方法不知道。



to_s 在控制台记录 query.first 的结果时使用的方法在activerecord中的某个位置定义,并且使用为数据库中的模型定义的属性。由于您的属性仅是为 Link 的特定实例定义的,而不是为模型 Link 的模型定义的。



我发现这很有趣。以下是如何构造控制台中显示的消息的说明。它始于gem active_attr ,下面显示了以下三种方法:

  def检查
attribute_descriptions = attribute.sort.map {|键,值| #{key}:#{value.inspect}} .join(,)
分隔符=,除非attribute_descriptions.empty?
#<#{self.class.name} ## separator}#{attribute_descriptions}>
结束

#...

def属性
attributes_map {| name |发送名称}
结束

#...

def attributes_map
Hash [self.class.attribute_names.map {| name | [name,yield(name)]}]
end

方法 attributes_names 在gem activerecord

 <$ c中定义$ c> def attribute_names 
@attribute_names || =如果!abstract_class? && table_exists?
column_names
else
[]
end
end

#...

def column_names
@column_names || = @ connection.columns(@table_name).collect {| c | c.name}
结尾

这就是为什么您的笔数不显示的原因。 / p>

如果您确实希望它显示在控制台中,则可以覆盖 inspect 方法并将其添加到其中。 / p>

I have query like this: query = Link.select('url, max(created_at) as created_at, count(*) as url_count').group(:url).order('url_count desc, created_at asc')

Sample results of query.results.first:

2.2.0 :002 > query.first
 => #<Link id: nil, url: "http://1", created_at: "2015-03-10 16:43:54"> 

Why there is no url_count here, even though I know it is.

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

解决方案

The count is there all along but the model to_s method does not know about it.

The to_s method which is used when your console logs the result from query.first is defined somewhere in activerecord and it uses the attributes defined for the model in the database. Since your attribute is only defined for this particular instance of Link not for the model Link.

I found this quite interesting. Below is a description of how the message displayed in your console is constructed. It starts out in the gem active_attr with these 3 methods displayed below:

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

the method attributes_names is defined in the 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

And that is why your count does not show up.

If you really want it to show up in your console you could override the inspect method and add it there.

这篇关于为什么组计算字段不显示在查询结果中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆