Miguel Angel Quintana
Miguel Angel Quintana

Reputation: 1399

Display time remaining with distance_of_time_in_words

So i want item to display to remaining on Items after 7 days the item will be deleted. ive tried

 <%= distance_of_time_in_words(item.created_at, item.created_at + 7.days) %> 

but all i get is "7 Days" on all items. Can anyone simply how this helper method works ?

Upvotes: 3

Views: 2529

Answers (1)

Martin Tournoij
Martin Tournoij

Reputation: 27822

Lets looks at the documentation to see what distance_of_time_in_words does:

distance_of_time_in_words(from_time, to_time = 0, options = {})
Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.

So it reports the time difference of the first argument and the second argument. Now, you're doing:

distance_of_time_in_words(item.created_at, item.created_at + 7.days)

The difference between item.created_at and item.created_at plus seven days is always ... seven days ;-)

I assume that this is something that will always be deleted after seven days? In that case, what you want, is the difference between the current date and the creation date plus seven days, which you can get with:

distance_of_time_in_words(Time.now, item.created_at + 7.days)

Upvotes: 6

Related Questions