Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails active record find where nil

I have a cost attribute for my product model and its type is float and everything works well. When creating a new product, a user can enter 0 or leave blank for a free product or enter the cost when its a paid product and so far that also works well

Now i am trying to scope free items, but since float is returning NIL for empty fields and i cannot return nil fields.

i tried

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

and it doesn't return fields that are blank (NIL) but fields with 0 only but i want to return both blank (NIL) fields and 0 fields. I know float fields in rails 3 return nil instead of null

like image 530
Jean Linux Avatar asked Nov 16 '25 14:11

Jean Linux


1 Answers

Try this:

scope :free, where(:cost => [nil,0] )
like image 129
Amit Thawait Avatar answered Nov 19 '25 03:11

Amit Thawait