Rails ActiveRecord:after_update和after_touch之间的区别



我不认为它们是同义词,但功能相似。

  1. after_update是在触摸时触发,还是只忽略触摸
  2. after_touch是触发更新,还是仅触发触摸
[11] pry(main)> Foo.after_touch -> { Rails.logger.debug "after_touch called" }
=> [Foo(id: integer, name: string, age: date, created_at: datetime, updated_at: datetime)]
[12] pry(main)> Foo.after_update -> { Rails.logger.debug "after_update called" }
=> [Foo(id: integer, name: string, age: date, created_at: datetime, updated_at: datetime)]
[13] pry(main)> Foo.last.update name: "New name"
Foo Load (0.1ms)  SELECT "foos".* FROM "foos" ORDER BY "foos"."id" DESC LIMIT ?  [["LIMIT", 1]]
TRANSACTION (0.0ms)  begin transaction
Foo Update (0.2ms)  UPDATE "foos" SET "name" = ?, "updated_at" = ? WHERE "foos"."id" = ?  [["name", "New name"], ["updated_at", "2022-08-10 21:10:37.296537"], ["id", 2]]
after_update called
TRANSACTION (0.8ms)  commit transaction
=> true
[14] pry(main)> Foo.last.touch
Foo Load (0.1ms)  SELECT "foos".* FROM "foos" ORDER BY "foos"."id" DESC LIMIT ?  [["LIMIT", 1]]
TRANSACTION (0.0ms)  begin transaction
Foo Update (0.2ms)  UPDATE "foos" SET "updated_at" = ? WHERE "foos"."id" = ?  [["updated_at", "2022-08-10 21:08:09.188057"], ["id", 2]]
after_touch called
TRANSACTION (0.7ms)  commit transaction
=> true

最新更新