开发者

Database agnostic now() datetime function?

I've the following condition in one of my model which works for mysql.

with_scope :find => { :conditions => "starts_at <= now(开发者_JS百科) AND ends_at >= now()" }

But I get error on the sqlite3 test database coz that now() is specific to mysql db only. What would be a db agnostic way for that now() function?


CURRENT_TIMESTAMP is standard in SQL92, and should be sufficiently cross-platform.


Or you can use ruby to give you the current timestamp and not rely on the database. eg.

current_ts = Time.now
with_scope :find => { :conditions => ["starts_at <= ? AND ends_at >= ?", current_ts, current_ts] }

I personally prefer this as the timestamp can be modified easily (plus or minus an hour etc.) without having to write sql which may not work in all versions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜