繁体   English   中英

Rails将DateTime转换为'where'查询中的日期

[英]Rails convert DateTime to date in 'where' query

我需要对数据库进行查询并按开始日期过滤事件,但列类型是DateTime。 我在模型中做了范围:

scope :day, -> (start_date) {where start_date: start_date}

它适用于相同的DateTime值,但我需要一个过滤器来仅按日期获取Event ,而不是DateTime。

我有PG数据库并尝试:

  scope :day, -> (start_date) {where("start_date:date =?", "#{start_date.to_date}")}  

但是我收到了一个错误

如果要将数据库值强制转换为日期,则必须为不同的数据库服务器执行不同的操作,例如mysql的以下内容:

scope :day, -> (start_date) {where("DATE(start_date) = ?", start_date)}

因此,将start_date参数强制转换为日期时间可能更为合理:

scope :day, -> (start_date) {where start_date: start_date.to_datetime}

此外,BigRon在开始和结束之间发布了一个很好的过滤方法,Deep 发布了Rails 5.1的帮助程序,进一步简化了这种方法,但如果你想按日期过滤,你真的应该考虑你的数据库列是否真的需要是datetime单独。

你可以这样做:

使用SQL Date函数(取决于数据库,这是用于MySQL ):

scope :on_day -> (start_date) { where("DATE(start_date) = ?", start_date.to_date) }

或者使用范围(所以这将使用日期和时间来检查,只使用当天的开始和结束时间并执行BETWEEN查询):

scope :day, -> (start_date) { where start_date: start_date.beginning_of_day..start_date.end_of_day }

或者如果在Rails 5.1上 (这是Rails 5.1中引入的新助手):

scope :day, -> (start_date) { where start_date: start_date.all_day }

您可以在一天的开头到一天结束时使用范围:

scope :day, -> (start_date) {where start_date: start_date.beginning_of_day..start_date.end_of_day}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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