Search code examples
ruby-on-railsrubydevisedevise-confirmable

DateTime Math 2 Hours Or Greater Since confirmation_sent_at Rails Devise


Time and date math always seems to get the best of me. I want to write a check to verify it's been 2 hours or more since we sent the original devise confirmation email.

I've tried different variations trying to use DateTime and time etc and I cannot figure it out and could use some help. confirmation_sent_at is a DateTime I believe. EX: Tue, 17 May 2022 20:47:51.290039000 UTC +00:00.

How do I check if its been >= 2 hours since current_user.confirmation_sent_at . For testing purposes I've been using minutes and seconds fwiw 2.minutes.ago, 2.hours.ago etc.


Solution

  • For example now is 10:00

    And for example confirmation for user1 was sent in 9:00, for user2 in 5:00

    Using <

    user1.confirmation_sent_at < 2.hours.ago # => false
    user2.confirmation_sent_at < 2.hours.ago # => true
    

    And using >

    user1.confirmation_sent_at > 2.hours.ago # => true
    user2.confirmation_sent_at > 2.hours.ago # => false