Reputation: 7998
I've got a method I'm calling expired? which is simply meant to check to see if its been more than 24 hours since my object has been updated, and if so return true. Here is my method, but although it works, it feels dirty, can anybody think of a better way to achieve my goal?
DAY_IN_SECS = 86400
def expired?
return true unless (Time.now <=> self.updated_at + DAY_IN_SECS) == -1
end
Upvotes: 1
Views: 418
Reputation: 146053
Even in plain Ruby you could still do the simpler:
def expired?
Time.now > self.updated_at + DAY_IN_SECS
end
Upvotes: 5
Reputation: 11299
You're working with Rails, so you can use Rails Date/Time methods :).
For your problem, I like this :
def expired?
updated_at.advance(:days => 1).past?
end
Upvotes: 3
Reputation: 3529
You could use the active_support
functionality, so:
def expired?
self.updated_at > 1.day.ago
end
Upvotes: 12