简体   繁体   English

为什么.where(:attribute => nil)检查nil值?

[英]Why .where(:attribute => nil) checks for nil value?

So here is the query, very simple: 所以这是查询,非常简单:

field = :has_bill
value = nil
scoped.where(field => value)

And it outputs as: 输出为:

(`electricity_profile_segment_summaries`.`has_swimming_pool` IN ('') OR `electricity_profile_segment_summaries`.`has_swimming_pool` IS NULL)

Which includes nil values and 0 values as well. 其中包括nil值和0值。 Which is not correct, I would like to represent only NULL values from the table. 这是不正确的,我只想表示表中的NULL值。

Any help appreciated 任何帮助表示赞赏

How about manually limiting to NULL for the nil case? 手动将nil情况限制为NULL怎么样?

if value.nil?
  scoped.where( "#{field} IS NULL" )
else
  scoped.where( field => value
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM