gpt4 book ai didi

mysql - rails 将日期时间转换为范围内的日期

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:09 24 4
gpt4 key购买 nike

在我的模型中,我定义了以下范围:

scope :today, -> { where time_start: Date.today }

这不起作用,因为我正在将 mysql 日期时间与日期进行比较。我正在尝试执行与 date() mysql 函数等效的操作(从日期时间中提取日期)。

最佳答案

试试这个:

scope :today, -> { where time_start: Date.today.beginning_of_day..Date.today.end_of_day }

在条件中使用范围将生成一个 BETWEEN 查询,如下所示:

SELECT "models".* FROM "models"
WHERE (
"models"."time_start" BETWEEN
'2016-07-29 00:00:00.000000' AND '2016-07-29 23:59:59.999999'
)

关于mysql - rails 将日期时间转换为范围内的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com