Rails活动记录找到nil

Jea*_*nux 2 ruby activerecord ruby-on-rails

我的产品型号有一个成本属性,其类型是float一切正常.在创建新产品时,用户可以为免费产品输入0或留空,或者在付费产品时输入成本,到目前为止也能正常运行

现在我想尝试范围免费项目,但因为浮动返回NIL空字段,我不能返回空字段.

我试过了

scope :free, where("cost IS NULL or cost < ?", 1 )

并且它不返回空白字段(NIL)但仅返回0字段,但我想返回空白(NIL)字段和0字段.我知道rails 3中的float字段返回nil而不是null

Ami*_*ait 6

试试这个:

scope :free, where(:cost => [nil,0] )
Run Code Online (Sandbox Code Playgroud)