为什么Relation.size有时会在Rails 4中返回Hash

时间:2015-01-15 02:59:46

标签: ruby-on-rails-4 activerecord arel

我可以用两种不同的方式运行查询来返回一个Relation。

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

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

  

未定义的方法`零?' for {}:Hash

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

以下是一段代码,其中包含两个查询的looging语句以及生成的日志

CODE:

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("-----------------------------------------------------------")

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
-----------------------------------------------------------

1 个答案:

答案 0 :(得分:10)

size对象上的

ActiveRecord::Relation会转换为count,因为前者会尝试获取Relation的计数。但是当您在分组的count对象上调用Relation时,您会收到一个哈希值。

此哈希的键是分组列的值;此哈希的值是各自的计数。

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?