Rails选择日期大于或等于nil的记录

时间:2015-06-27 07:03:19

标签: ruby-on-rails ruby activerecord

我的Product模型带有expired_at日期字段。我想查询返回所有expired_at值大于某个值或等于nil的产品。

但我遇到了麻烦:

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

如何在查询中获取这些产品?

1 个答案:

答案 0 :(得分:11)

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

试试这个