为什么 Relation.size 有时 return 是 Rails 中的哈希 4

Why does Relation.size sometimes return a Hash in Rails 4

我可以 运行 以两种不同的方式查询 return 关系。

当我询问关系的大小时,一个查询给出了预期的 Fixnum,另一个给出了一个 Hash,它是 Relations Group By 语句中每个值的散列以及每个值的出现次数。

在 Rails 3 我假设它总是 returned 一个 Fixnum 因为我从来没有遇到过问题而 Rails 4 它有时 return 是一个哈希和一个声明喜欢 Rel.size.zero?给出错误:

undefined method `zero?' for {}:Hash

我最好只使用 .blank 吗?检查零记录以确保避免意外错误的方法?

这是一段代码,其中包含两个查询的搜索语句和生成的日志

代码:

assessment_responses1=AssessmentResponse.select("process").where("client_id=? and final = ?",self.id,false).group("process")
logger.info("-----------------------------------------------------------")
logger.info("assessment_responses1.class = #{assessment_responses1.class}")
logger.info("assessment_responses1.size.class = #{assessment_responses1.size.class}")
logger.info("assessment_responses1.size value = #{assessment_responses1.size}")

logger.info("............................................................")

assessment_responses2=AssessmentResponse.select("distinct process").where("client_id=? and final = ?",self.id,false)
logger.info("assessment_responses2.class = #{assessment_responses2.class}")
logger.info("assessment_responses2.size.class = #{assessment_responses2.size.class}")
logger.info("assessment_responses2.size values = #{assessment_responses2.size}")
logger.info("-----------------------------------------------------------")

日志

-----------------------------------------------------------
assessment_responses1.class = ActiveRecord::Relation::ActiveRecord_Relation_AssessmentResponse
   (0.5ms)  SELECT COUNT(`assessment_responses`.`process`) AS count_process, process AS process FROM `assessment_responses` WHERE `assessment_responses`.`organisation_id` = 17 AND (client_id=43932 and final = 0) GROUP BY process
assessment_responses1.size.class = Hash
  CACHE (0.0ms)  SELECT COUNT(`assessment_responses`.`process`) AS count_process, process AS process FROM `assessment_responses` WHERE `assessment_responses`.`organisation_id` = 17 AND (client_id=43932 and final = 0) GROUP BY process
assessment_responses1.size value = {"6 Month Review(1)"=>3, "Assessment(1)"=>28, "Assessment(2)"=>28}
............................................................
assessment_responses2.class = ActiveRecord::Relation::ActiveRecord_Relation_AssessmentResponse
   (0.5ms)  SELECT COUNT(distinct process) FROM `assessment_responses` WHERE `assessment_responses`.`organisation_id` = 17 AND (client_id=43932 and final = 0)
assessment_responses2.size.class = Fixnum
  CACHE (0.0ms)  SELECT COUNT(distinct process) FROM `assessment_responses` WHERE `assessment_responses`.`organisation_id` = 17 AND (client_id=43932 and final = 0)
assessment_responses2.size values = 3
-----------------------------------------------------------
ActiveRecord::Relation 对象上的

size 转换为 count,因为前者试图获取 Relation 的计数。但是当你在分组的 Relation 对象上调用 count 时,你会收到一个散列。

这个散列的键是分组列的值;此散列的值是各自的计数。

AssessmentResponse.group(:client_id).count # this will return a Hash
AssessmentResponse.group(:client_id).size # this will also return a Hash

这适用于 following methodscountsumaveragemaximumminimum

如果要检查行是否存在,只需使用 exists? 即执行以下操作:

AssessmentResponse.group(:client_id).exists?

而不是这个:

AssessmentResponse.group(:client_id).count.zero?