Mongoid named scope, comparing two time fields in one document

I need to create a named object in Mongoid that compares two Time fields in one document. For instance,

scope :foo, :where => {:updated_at.gt => :checked_at}

This, obviously, will not work, since it is considered :checked_atas a symbol, not an actual field. Any suggestions on how to do this?

Update 1

Here is my model where I declare this area, with lots of redundant code.

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  field :checked_at, :type => Time

  scope :unresolved, :where => { :updated_at.gt => self.checked_at }
end

This gives me the following error:

'<class:User>': undefined method 'checked_at' for User:Class (NoMethodError)

+5
source share
3 answers

As far as I know, mongodb does not support queries against dynamic values. But you can use javascript function:

scope :unresolved, :where => 'this.updated_at >= this.checked_at'

, is_unresolved, true , ( ).

+7
scope :foo, :where => {:updated_at.gt => self.checked_at}

, :

scope :foo, where(:start_date.lte=>Date.today.midnight)
+3

, , , .

class User
  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  field :checked_at, :type => Time

  scope :unresolved, lambda{ |user| where(:updated_at.gt => user.checked_at) }
end

User.unresolved(my_user_object) , , , , , , . , , , MapReduce , , Baju ( )

+1

All Articles