Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby comparing dates

I would like to do is to know if a user has been created in the system in the last 10 second. so i would do:

  def new_user
    if(DateTime.now - User.created_at < 10)
      return true
    else 
      return false 
    end
  end

IT is just an idea , how can i do it correctly? thank you

like image 923
fenec Avatar asked May 16 '10 01:05

fenec


2 Answers

class User < ActiveRecord::Base
  def new?
    created_at > 10.seconds.ago
  end
end

# Example:
user = User.create!
user.new?
#=> true

sleep 11
user.new?
#=> false

(Presuming your User class is an ActiveRecord model.)

like image 82
molf Avatar answered Oct 25 '22 12:10

molf


User.created_at > 10.seconds.ago
like image 25
Jakub Hampl Avatar answered Oct 25 '22 12:10

Jakub Hampl