Trip Trip - 1 year ago 79
Ruby Question

How do you subtract from a datetime?

I'd like to write a simple function to say in hours:

How much time has passed since this has been created?

My attempts:

-time = - (self.created_at.hour)

Does anyone know how to do this in Ruby on Rails?

Answer Source

Rails usually uses Time, not DateTime. Why don't you do - self.created_at? Then you can convert from seconds to hours by dividing by 3600.