views:

66

answers:

2

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

with_scope :find => { :conditions => "starts_at <= now() 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?

+4  A: 

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

Michael Petrotta
+1  A: 

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.

Josh K
I edited your post:Two things: 1) Don't need zone, it's automatically converted to UTC unless you purposefully set it not to be 2) don't need to to_s(:db) it, that is also done automatically.
Ryan Bigg
thanks radar, looks much neater now.
Josh K