I would like to modify updated_at attribute so that every time record is updated it would only show date and hours while hours and minutes being zeros. Instead of 2010-08-13 11:04:12, there would be 2010-08-13 11:00:00. What is the most rational way to do this in Rails 2.3?
Update
The reason why I want to do what I have asked is because I would like to sort data, by date with hours and seconds omitted.
Thanks!
Don’t try to change the value stored in the database; change how you are outputting it:
See the
strftimeruby doc for other format specifiers you can use.Based on your update, I would still not change how
updated_atis stored, but instead format the date within theORDER BYof your query. For instance, you couldI did a very quick profiling test on a live table in my application, which has about 22,000 records.
ORDER BY updated_attook between 2.94 and 3.19s to complete, with a mean time of 3.00s.ORDER BY DATE(updated_at), HOUR(updated_at)took between 2.93 and 3.06s to complete, with a mean time of 2.99s. Here’s the profiling data: