ruby-on-rails-4 – 为什么Relation.size有时会在Rails 4中返回Hash

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-4 – 为什么Relation.size有时会在Rails 4中返回Hash前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以用两种不同的方式运行查询来返回一个Relation.

当我询问Relation的大小时,一个查询按预期给出一个Fixnum,另一个给出一个Hash,它是Relations Group By语句中每个值的哈希值,每个值的出现次数.

在Rails 3中,我认为它总是返回一个Fixnum,因为我从来没有遇到过Rails 4的问题,它有时会返回一个Hash和一个像Rel.size.zero这样的语句?给出错误

undefined method `zero?’ for {}:Hash

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

这是一段代码,包含两个查询生成的日志的looging语句

码:

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 = ?",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("-----------------------------------------------------------")

LOG

-----------------------------------------------------------
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对象上的大小转换为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 methods:计数,总和,平均值,最大值和最小值.

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

AssessmentResponse.group(:client_id).exists?

而不是这个:

AssessmentResponse.group(:client_id).count.zero?
原文链接:https://www.f2er.com/ruby/270667.html

猜你在找的Ruby相关文章