简体   繁体   中英

Rails 3 & ActiveRecord - sql query about date

I build my simple blog system and every article in the system has two inputs for setting the the, from that the article will be visible for the visitors and the second input is for the date, till that will be visited.

For example, if I will set to this inputs the values

2011-10-25 and 2011-10-28 , so that's mean, the article will be visible 3 days. If I wanted to have an article still visible, so I would like to have in inputs the values 0000-00-00.

But now I think how to make an sql query - I try something like:

Article.where('from_date <= ? AND to_date >= ?', Func.today, Func.today)

This works me if I have set a date, from that and till that I want to have an article visible. But if I will set that I want to have an article visible everytime (0000-00-00), so this query doesn't works...

I would like to ask you about the hint, how to update the sql query above for it...

Thanks in advance,

Manny

不是将日期字段设置为全零,而是将它们设置为nil并在查询中查找空值。

Article.where('(from_date IS NULL OR from_date <= ?) AND (to_date IS NULL OR to_date >= ?)', Func.today, Func.today)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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