Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 7.days to human readable string

I know this looks trivial but let say that in Ruby on Rails I have

document.expire_in = 7.days

how can I print human readable version of the expiry mesages ?

"Document will expire in #{document.expire_in}"
=> Document will expire in 7 days

maybe something that works with I18n.t or I18n.l

the only way this works is

7.days.inspect => "7 days"

is this the only way ??

I'm looking at ActiveSupport::Duration and don't see an answer

thx

like image 552
equivalent8 Avatar asked Sep 13 '25 06:09

equivalent8


1 Answers

This doesn't answer your specific question but it seems to me that you would be better off setting the datetime that it expires and then leverage distance_of_time_in_words.

If you're always going to simply say 7 days, then why not just write that as a hard-coded string?

like image 71
Robert Avatar answered Sep 15 '25 23:09

Robert