轨道 3 和ActiveRecord - 关于日期的 sql 查询
我构建了我的简单博客系统,系统中的每篇文章都有两个用于设置的输入,从该输入开始,该文章将对访问者可见,第二个输入是日期,直到被访问为止。
例如,如果我将此输入设置为值
2011-10-25 和 2011-10-28,那么这意味着该文章将在 3 天后可见。如果我想让一篇文章仍然可见,那么我想在输入中输入值 0000-00-00。
但现在我想如何进行 sql 查询 - 我尝试如下操作:
Article.where('from_date <= ? AND to_date >= ?', Func.today, Func.today)
如果我设置了一个日期,从该日期到该日期,我希望有一篇文章可见,那么这对我有用。但是如果我设置我希望每次(0000-00-00)都有一篇文章可见,那么这个查询不起作用...
我想问你关于提示,如何更新上面的sql查询它...
预先感谢,
曼尼
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将日期字段设置为全零,而是将它们设置为 nil 并在查询中查找空值。
Instead of setting the date fields to all zeros, set them to nil and look for null values in the query.