大多数日期方法将根据当前时间或日期进行计算,并在X个月内跳转到该日期。但是,如果想要提取具有日照月内日期的所有对象,则可以使用
Product.where(['date >= ? AND date <= ?', @products, Date.today.at_beginning_of_month.next_month, Date.today.at_end_of_month.next_month]).all
服务器日志显示:
date >= '2013-12-01' AND date <= '2013-12-30'
但以下失败
Product.where(['date >= ? AND date <= ?', @products, Date.today.at_beginning_of_month.month+1, Date.today.at_end_of_month.month+2]).all
服务器日志显示:
date >= 12 AND date <= 13
我还假设有一个更好的速记可以陈述
date IN (?)
没有
答案 0 :(得分:1)
Date.today.month
返回一个整数,而不是日期。您可以执行(Date.today + 2.months).at_beginning_of_month
或2.months.from_now.at_beginning_of_month
是的,你可以通过一个范围进行BETWEEN选择:
Product.where(:date => Date.today.at_beginning_of_month..Date.today.at_end_of_month)
# => "SELECT \"products\".* FROM \"products\" WHERE (\"products\".\"date\" BETWEEN '2013-11-01' AND '2013-11-30')"