ruby - Strange Behavior with mongoid date range query. - Rails 3 -


i running in strange date range query problem. when use datetime.now.utc option query database, query date datetime.now different current datetime.now(i.e. 1 that's returned on console.)

my mogoid.yml uses-

use_utc: true 

i have named scope -

scope :running_auctions, {        :where => { :end_time.gt => datetime.now.utc },        :order_by => [:end_time, "asc"]  } 

and on console can -

loading development environment (rails 3.0.7)

irb(main):001:0> auction.running_auctions => #<mongoid::criteria selector: {:end_time=>{"$gt"=>fri jul 22 00:42:38 utc 2011}}, options:  {:sort=>[:end_time, "asc"]}, class:    auction, embedded: false> 

notice date here fri jul 22 00:42:38 utc 2011

irb(main):002:0> datetime.now => fri, 22 jul 2011 11:42:56 +0530  irb(main):003:0> datetime.now.utc => fri, 22 jul 2011 06:12:59 +0000 

notice here datetime fri, 22 jul 2011 06:12:59 +0000

what making query date older actual current date ? mogoid or mongodb doing caching there ? please let me know if missing something.

update

loading development environment (rails 3.0.7) irb(main):001:0> auction.running_auctions(datetime.now.utc) => #<mongoid::criteria  selector: {:end_time=>{"$gt"=>fri jul 22 01:21:53 utc 2011}},  options:  {:sort=>[:end_time, "asc"]},  class:    auction,  embedded: false>  irb(main):002:0> datetime.now.utc => fri, 22 jul 2011 06:52:03 +0000 

you have use lambda there. of now, datetime.now.utc computed @ application startup , cached application code.

you need write scope as:

scope :running_auctions, lambda {   {     :where => { :end_time.gt => datetime.now.utc },     :order_by => [:end_time, "asc"]   } } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -