更新updated_at时间而不更改任何内容

Dee*_*kor 12 ruby-on-rails ruby-on-rails-4

有什么方法可以触摸模型,导致updated_at字段更新到当前时间而不实际更改其他字段中的任何内容?

ush*_*sha 29

您可以使用 touch

model.touch
Run Code Online (Sandbox Code Playgroud)

要么

model.touch(:updated_at) #or anyother timestamp attribute
Run Code Online (Sandbox Code Playgroud)

文档在这里

  • 我遇到了这个问题,但我在一个 `after_touch` 回调中,所以调用 `touch` 会导致递归循环。 (2认同)