Wednesday, 11 September 2013

What is a good way to record time periods in Rails application, so that I can easily use them for calculations?

What is a good way to record time periods in Rails application, so that I
can easily use them for calculations?

I have time periods like:
2 years
1 month
4 days
I would like to add them to existing records in my DB in a way that I
could easily use them for calculations? The type of calculations I would
need would be [time_period] - (Time.now - [datetime]) in order to see how
much of the time period is left.
Since my periods are quite standard, I was thinking to save the time
periods as strings "2-years", "1-month", "4-days" etc. and split them on
use. For example "4-days" could become something like this:
4.send("days".to_sym)
What do you think about this method? Any better ideas?

No comments:

Post a Comment