[英]Ruby on Rails: Query where date equals another date (ignoring time)
I'm trying to execute this query on my model but I can't figure it out. 我正在尝试在我的模型上执行此查询,但我无法弄明白。
I have an array of date called "array_of_dates", I have built it from another model: 我有一个名为“array_of_dates”的日期数组,我是从另一个模型构建的:
array_of_dates = [] user.each do |user| array_of_dates << user.birthday_date end
I tried 我试过了
Event.where("date in (?)", array_of_dates)
However, date from Event also has a time set. 但是,来自Event的日期也有一个时间设置。 I would like to ignore the time that has been set here, because I am just looking for the day.
我想忽略这里设定的时间,因为我只是在寻找这一天。 I have tried parsing user.birthday_date to another format, but I don't know how to change the format of "date" from the Event model.
我尝试将user.birthday_date解析为另一种格式,但我不知道如何从Event模型更改“date”的格式。
Thank you for your help! 谢谢您的帮助!
First transform the array_of_dates
dates by converting each date into a range that covers the entire day: 首先通过将每个日期转换为涵盖整天的范围来转换
array_of_dates
日期:
array_of_dates = array_of_dates.map {|date| date.beginning_of_day..date.end_of_day}
This will convert each date into a range such as (Mon 27 Oct 2014 00:00:00..Mon 27 Oct 2014 23:59:59)
这会将每个日期转换为范围,例如
(Mon 27 Oct 2014 00:00:00..Mon 27 Oct 2014 23:59:59)
Now use the following query format to find the events: 现在使用以下查询格式来查找事件:
Event.where(date: array_of_dates)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.