Dont use time_ago_in_words
Rails provides a helper method time_ago_in_words to display the distance between one time and now, like “5 minute ago”, it’s very useful.
Before
It looks fine, but we have some room to improve.
- Your servers have to calculate the time ago for each request, it wastes the cpu capability on server side, why not move the calculation to client side.
- If we calculate the time ago on server side, it gets difficult to cache the page. e.g. after you create a comment, the time ago of the comment shows “5 seconds ago”, if you cache the page, the time ago still show “5 second ago” after 3 minute (depends on your cache strategy).
Refactor
The solution is to use browser script like javascript. On server side, what you need is to pass the created or updated time instead of calculated time ago, then the javascript will calculate the time ago on client side.
Here is a javascript solution based on jquery , of course, you can use other time ago js library.
Now, you save the server cpu capability and make your page easier to cache.