Don't try to change the value stored in the database; change how you are outputting it:
timestamp = @model.updated_at # where @model is your model object
# output it in a format that overrides the values for minute and second
puts timestamp.strftime "%Y-%m-%d %H:00:00"
See the strftime
ruby doc for other format specifiers you can use.
Based on your update, I would still not change how updated_at
is stored, but instead format the date within the ORDER BY
of your query. For instance, you could
ORDER BY DATE(updated_at), HOUR(updated_at), ...
I did a very quick profiling test on a live table in my application, which has about 22,000 records. ORDER BY updated_at
took 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:
+----------+------------+-----------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------+
| 1 | 2.94530500 | SELECT * FROM posts ORDER BY updated_at |
| 2 | 2.94583800 | SELECT * FROM posts ORDER BY updated_at |
| 3 | 3.18711700 | SELECT * FROM posts ORDER BY updated_at |
| 4 | 2.96923700 | SELECT * FROM posts ORDER BY updated_at |
| 5 | 2.97255400 | SELECT * FROM posts ORDER BY updated_at |
| 6 | 3.06706800 | SELECT * FROM posts ORDER BY DATE(updated_at), HOUR(updated_at) |
| 7 | 3.00414400 | SELECT * FROM posts ORDER BY DATE(updated_at), HOUR(updated_at) |
| 8 | 2.95551500 | SELECT * FROM posts ORDER BY DATE(updated_at), HOUR(updated_at) |
| 9 | 3.02181900 | SELECT * FROM posts ORDER BY DATE(updated_at), HOUR(updated_at) |
| 10 | 2.93130000 | SELECT * FROM posts ORDER BY DATE(updated_at), HOUR(updated_at) |
+----------+------------+-----------------------------------------------------------------+