Skip to content

Does not work with database now() :) #7

szymon-przybyl opened this Issue Aug 21, 2011 · 7 comments

3 participants



I was trying to test my expiring activeness of product (just scope in activerecord):

scope :active, where('expires_at > now()')

but Delorean only travels in ruby time, not in db time :) I know it is highly impossible to mock db time. but maybe somebody have some idea about it? maybe it is better not to use database specific methods, such as random() or now(), but pass for example converted to database date format? something like this:

scope :active, where("expires_at > #{}")

Actually i've tried to pass like this:

class Product < ActiveRecord::Base
  scope :active, where('expires_at IS NOT NULL AND expires_at > ?',

but it looks like it something breaks Delorean, becouse this code:

Delorean.jump(7.days) { puts }

gives me:

SELECT "products".* FROM "products" WHERE (expires_at IS NOT NULL AND expires_at > '2011-08-21 20:54:10.003969')
SELECT "products".* FROM "products" WHERE (expires_at IS NOT NULL AND expires_at > '2011-08-21 20:54:10.003969')

but with normal use of

def active?
  expires_at.present? and expires_at >

Delorean works fine. Any ideas why scope with disables Delorean time changes? I'm using postgres.

BeBanjo member
cavalle commented Aug 22, 2011

It might be the case that Delorean mocks but not

Patches welcomed :)

BeBanjo member
cavalle commented Aug 22, 2011

BTW, I don't think Delorean will ever be able to mock database's "now()" function


I'm using and it looks like Delorean mocks it fine, the problem appears when I pass to database query - then mocking does not work for some reason.

I'm using instead of, becouse it looks like my database is using UTC time for timestamps, and as far as i know returns UTC time, am I right? Or maybe my database is storing timestamps in my local time and returns UTC?

BeBanjo member
cavalle commented Aug 22, 2011

Ok. That's because in your example is executed in initialization time which probably is not what you want. You can try using a lambda:

scope :active, lambda { where('expires_at IS NOT NULL AND expires_at > ?', }

I'm closing this issue since no changes in Delorean are required

@cavalle cavalle closed this Aug 22, 2011

Even that does not work, that in that scope seems to be frozen somehow :| It does not changes within time_travel_to block of Delorean.

I've just substracted some days from my product's expires_at date :)

pierrea commented Oct 18, 2014

Few years later I faced with this issue.
I came up with this workaround to be able to test my query with Delorean:

(...) .where("expires_at > ?::timestamp",

This is a trivial example but I had to manipulate times in Postgres with now(). I couldn't just inject my ruby time instead without casting it with ::timestamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.