代码之家  ›  专栏  ›  技术社区  ›  tfe

ActiveRecord touch(:column)也总是更新:updated\u at

  •  3
  • tfe  · 技术社区  · 14 年前

    http://www.ruby-forum.com/topic/197440 .

    我想摸一摸柱子( :touched_at )没有自动更新 :updated_at

    我想这可能与我使用的特定型号有关,所以我尝试了几个不同的型号,结果相同。

    :更新于 当碰到另一列时? touch 使用 write_attribute

    编辑:

    一些澄清。。。Rails 2.3.5文档 触摸

    1 回复  |  直到 14 年前
        1
  •  8
  •   François Beausoleil    14 年前

    def touch!
      self.class.update_all({:touched_at => Time.now.utc}, {:id => self.id})
    end
    

    UPDATE posts SET touched_at = '2010-01-01 00:00:00.0000' WHERE id = 1
    

    这就是你想要的。如果您调用#save,这将以调用 #create_with_timestamps #update_with_timestamps

    顺便说一下 #touch