【发布时间】:2010-12-14 07:38:24
【问题描述】:
我的模型之一适用于 mysql。
with_scope :find => { :conditions => "starts_at <= now() AND ends_at >= now()" }
但我在 sqlite3 测试数据库上遇到错误,因为 now() 仅特定于 mysql db。 对于 now() 函数,什么是与 db 无关的方式?
【问题讨论】:
标签: mysql ruby-on-rails sqlite
我的模型之一适用于 mysql。
with_scope :find => { :conditions => "starts_at <= now() AND ends_at >= now()" }
但我在 sqlite3 测试数据库上遇到错误,因为 now() 仅特定于 mysql db。 对于 now() 函数,什么是与 db 无关的方式?
【问题讨论】:
标签: mysql ruby-on-rails sqlite
CURRENT_TIMESTAMP 在 SQL92 中是标准的,应该足够跨平台。
【讨论】:
或者您可以使用 ruby 为您提供当前时间戳,而不依赖于数据库。例如。
current_ts = Time.now
with_scope :find => { :conditions => ["starts_at <= ? AND ends_at >= ?", current_ts, current_ts] }
我个人更喜欢这个,因为可以轻松修改时间戳(加减一小时等),而无需编写可能不适用于所有版本的 sql。
【讨论】: