覆盖 Rails Updated_at 属性

发布于 2024-09-15 08:26:58 字数 233 浏览 2 评论 0原文

我想修改 updated_at 属性,以便每次更新记录时,它只会显示日期和小时,而小时和分钟为零。 不再是 2010-08-13 11:04:12,而是 2010-08-13 11:00:00。在 Rails 2.3 中执行此操作最合理的方法是什么?

更新

我想做我所要求的事情的原因是因为我想按日期对数据进行排序,省略小时和秒。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原来是傀儡 2024-09-22 08:26:58

不要尝试更改数据库中存储的值;更改输出方式:

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"

请参阅 strftime< /a> ruby​​ doc 用于您可以使用的其他格式说明符。


根据您的更新,我仍然不会更改 updated_at 的存储方式,而是在查询的 ORDER BY 中格式化日期。例如,您可以

ORDER BY DATE(updated_at), HOUR(updated_at), ...

对我的应用程序中的实时表进行非常快速的分析测试,该表包含大约 22,000 条记录。 ORDER BY Updated_at 需要 2.94 到 3.19 秒才能完成,平均时间为 3.00 秒。 ORDER BY DATE(updated_at), HOUR(updated_at) 需要 2.93 到 3.06 秒才能完成,平均时间为 2.99 秒。这是分析数据:

+----------+------------+-----------------------------------------------------------------+
| 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) |
+----------+------------+-----------------------------------------------------------------+

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) |
+----------+------------+-----------------------------------------------------------------+
一百个冬季 2024-09-22 08:26:58

它是一个时间戳对象,因此您最好只使用辅助函数来截断分/秒信息。

It's a time stamp object so your probably best off just to use a helper function to truncate the min/sec info.

念﹏祤嫣 2024-09-22 08:26:58

whatever.created_at.strftime("%Y-%m-%d %H:00:00")

whatever.created_at.strftime("%Y-%m-%d %H:00:00")

挽清梦 2024-09-22 08:26:58

我会推荐丹尼尔的解决方案。但是,如果您100%肯定想要使用修改后的“updated_at”字段保存记录,则必须为给定模型重新实现rails的自动时间戳。

因此,在您的模型中,您可以执行以下操作:

self.record_timestamps = false # disables rails' auto-timestamping
before_create :set_created_at
before_save :set_updated_at  

def set_created_at
  self.created_at = Time.now
end
def set_updated_at
  self.updated_at = Time.now.change({:min => 0, :sec => 0}) # clears minutes and seconds
end

I would recommend Daniel's solution. However, if you're 100% positive that you want to save the record with your modified "updated_at" field, you'll have to re-implement rails' auto-timestamping for the given model.

So, in your model, you can do the following:

self.record_timestamps = false # disables rails' auto-timestamping
before_create :set_created_at
before_save :set_updated_at  

def set_created_at
  self.created_at = Time.now
end
def set_updated_at
  self.updated_at = Time.now.change({:min => 0, :sec => 0}) # clears minutes and seconds
end
七秒鱼° 2024-09-22 08:26:58

这不是您应该对活动记录执行的操作。这是视图助手可以完成的事情,并且您不需要改变保存内容的方式,这可能只是将来更多的工作。

考虑一下 strftime 以及如何改变它。

在帮助文件中:

def format_date_for_rounded_hour(date)
    date.striftime("%Y-%m-%d %H:00")
end

This isn't something you should do with active record. It's something that a view helper could accomplish and you don't need to alter the way things are getting saved which will probably just be more work in the future.

Think about strftime and how you can alter that instead.

In a helper file:

def format_date_for_rounded_hour(date)
    date.striftime("%Y-%m-%d %H:00")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文