9

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

2 Answers 2

10
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.)

Sign up to request clarification or add additional context in comments.

Comments

3
User.created_at > 10.seconds.ago

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.