gpt4 book ai didi

ruby-on-rails - 基于小时的 ActiveRecord 范围

转载 作者:行者123 更新时间:2023-11-29 14:09:41 26 4
gpt4 key购买 nike

迭代

<% @challenges.with_exact.each do |challenge| %>
etc...
<% end %>

模型范围尝试

scope :with_exact, -> { where("exact == ?", Time.zone.now.hour) } # Error: PG::UndefinedFunction: ERROR:  operator does not exist: time without time zone == integer
scope :with_exact, -> { where(exact: Time.zone.now.hour) } # Error: PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type time: "13"

一些挑战将有一个 nil exact。

我如何才能仅在 Time.zone.now.strftime('%H') == challenge.exact.strftime('%H') 处重复挑战

最佳答案

您可以使用 date_part(text, timestamp) postgres 函数:

where("date_part('hour', exact) = ?", Time.zone.now.hour)

这里还有等。

还有另一种方式 extract(field from timestamp)

where("extract(hour from exact) = ?", Time.zone.now.hour)

关于ruby-on-rails - 基于小时的 ActiveRecord 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362017/

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