“触摸"Rails 2.3.2 中的 updated_at 列 [英] "Touch" updated_at column in Rails 2.3.2
问题描述
我有一个过程,它遍历一堆 ActiveRecord 模型,进行一些处理,然后再次保存模型.但是,处理过程通常不会导致属性发生更改,因此 updated_at
列永远不会更改(即使调用了 save).
我不想禁用部分更新(通常,它们很有用).我猜我的两个选择是:
- 向模型添加一个单独的时间戳列(即
processed_at
")并自己管理它,尽管这看起来有点浪费/冗余. - 或者以某种方式覆盖对
updated_at
属性的管理?
我听说 Rails 3 将有一个触摸"方法,这正是我正在寻找的.p>
任何想法/选项/意见?
实际上是 touch
方法已经在 Rails 2.3.x 中了,所以你可以简单地做:
model.touch
更新updated_at
列.或者,使用当前日期和时间更新其他列:
model.touch(:column_name)
I have a process which iterates through a bunch of ActiveRecord models, does some processing, and saves the models again. Often though, the processing doesn't result in an changes to the attributes and so the updated_at
column never changes (even though save is called).
I'd prefer not to disable partial updates (in general, they're useful). I'm guessing that my two options are to:
- Add a separate timestamp column to the model (i.e., '
processed_at
') and manage this myself, although this seems a bit wasteful/redundant. - Or somehow override the management of the
updated_at
attribute?
I've heard that Rails 3 will have a 'touch' method which would be exactly what I'm looking for.
Any ideas/options/opinions?
Actually the touch
method is already in Rails 2.3.x, so you can simply do:
model.touch
To update the updated_at
column. Alternatively, to update some other column with the current date and time use:
model.touch(:column_name)
这篇关于“触摸"Rails 2.3.2 中的 updated_at 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!