Rails 2.3.2中的“ Touch” updated_at列
收藏

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

我宁愿不要禁用部分更新(通常,它们很有用)。我猜我的两个选择是:

  1. Add a separate timestamp column to the model (i.e., 'processed_at') and manage this myself, although this seems a bit wasteful/redundant.
  2. Or somehow override the management of the updated_at attribute?

我听说Rails 3会提供一种“触摸”方法,而这正是我在寻找的方法。

有什么想法/选择/意见吗?

最佳答案

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)

    公众号
    关注公众号订阅更多技术干货!