regmiprem
regmiprem

Reputation: 735

Time difference condition in rails using database Mongodb

I want such condition that after for example 5 days of data creation user cannot edit that data again. Since i am using Mongodb as database

include Mongoid::Timestamps

which save database in

 "created_at" : ISODate("2013-03-28T05:19:55.418Z")       in this format.

I want any idea to make condition that can check 5 days to this created_at date format.

Upvotes: 1

Views: 288

Answers (2)

Moacy Barros
Moacy Barros

Reputation: 1947

you can check out only documents from 4 day ago or earlier:

db.datecol.find({ "date" : { $gte : (new Date((new Date()).getTime() - (4* 24 * 60 * 60 * 1000))) } } ).sort( { "date": 1 } )

The query will get all documents on datecol collection which "date" filed is earlier than o equals to 4 days ago.

Regards, Moacy

Upvotes: 1

Sascha Kaestle
Sascha Kaestle

Reputation: 1293

I would try to do this via a validation

class UserInfo
  include Mongoid::Document
  include Mongoid::Timestamps

  validate :save_within_5_days_of_creation

  def save_within_5_days_of_creation
    self.created_at + 5.days < Time.now 
  end
end

I'm not sure about whether you can check the Mongoid::Timestamps directly against Time.now, but I think you should be able to

Upvotes: 0

Related Questions