迭代
<% @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
个。
如何仅在Time.zone.now.strftime('%H') == challenge.exact.strftime('%H')
答案 0 :(得分:4)
您可以使用date_part(text, timestamp)
postgres功能:
where("date_part('hour', exact) = ?", Time.zone.now.hour)
此处还有month
,year
等等。
where("extract(hour from exact) = ?", Time.zone.now.hour)