Ant*_*com 0 ruby postgresql ruby-on-rails
迭代
<% @challenges.with_exact.each do |challenge| %>
etc...
<% end %>
Run Code Online (Sandbox Code Playgroud)
模型范围尝试
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"
Run Code Online (Sandbox Code Playgroud)
有些挑战会有一个nil确切的。
我如何才能仅在何处迭代挑战 Time.zone.now.strftime('%H') == challenge.exact.strftime('%H')
您可以使用date_part(text, timestamp)postgres 函数:
where("date_part('hour', exact) = ?", Time.zone.now.hour)
Run Code Online (Sandbox Code Playgroud)
这里还有month,year等等。
还有另一种方式 extract(field from timestamp)
where("extract(hour from exact) = ?", Time.zone.now.hour)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |