加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何查找查询结果的大小

发布时间:2020-12-17 01:35:53 所属栏目:百科 来源:网络整理
导读:我在rails中有以下查询: records = Record.select('y_id,source') .where(:source = source,:y_id = y_id) .group(:y_id,:source) .having('count(*) = 1') 如果我放置记录,我会得到以下输出: [#记录来源:“XYZ”,y_id:10000009,#记录来源:“XYZ”,y_id
我在rails中有以下查询:

records = Record.select('y_id,source')
                .where(:source => source,:y_id => y_id)
                .group(:y_id,:source)
                .having('count(*) = 1')

如果我放置记录,我会得到以下输出:
[#<记录来源:“XYZ”,y_id:10000009>,#<记录来源:“XYZ”,y_id:10000070>]

这看起来像输出数组中有2个元素.但是当我尝试做record.size时,我得到:
{[10000009,“XYZ”] => 1,[10000070,“XYZ”] => 1}

  • Why doesn’t records.size print 2 when records is an array having 2 elements? Is the group by query result behaving differently for some reason?

  • What should I do to get the size of records

解决方法

我可能走错了路,但我认为问题与.size的工作方式有关.

Size将自动尝试确定是否调用.count或.length.这些行为如下:

> .count执行SQL COUNT
> .length计算结果数组的长度

然而,在occassion .size将返回一个哈希(因为它决定使用.count)

所以你的解决方案可能是使用.length,它将返回一个整数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读