Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails select records with date bigger than or equal to nil

I have a Product model with expired_at date field. I want query which return all products with expired_at value that bigger than some value or equals to nil.

But I met a troubles here:

manufactured_at = Date.tomorrow
Product.where('expired_at > ? OR expired_at = NULL', manufactured_at)
# => ActiveRecord::Relation []
Product.all
# => Product id: 1, expired_at: nil, Product id; 2, expired_at: nil, ...

How can I fetch these products in my query?

like image 255
asiniy Avatar asked Jun 27 '15 07:06

asiniy


1 Answers

manufactured_at = Date.tomorrow
Product.where("expired_at > ? OR expired_at IS NULL", manufactured_at)

try this

like image 146
Vrushali Pawar Avatar answered Nov 18 '22 21:11

Vrushali Pawar