DEV Community

@kon_yu
@kon_yu

Posted on

4 1

Convenient because updated_at does not update without changing the attributes of ActiveRecord and does not issue any extra SQL.

Overview

Even if the update method is executed without changing the attribute value in the model that inherits ActiveRecord, the update SQL itself is not executed.
UPDATE_AT with the update date is also not updated.

If the model is not changed, the SQL itself is not executed even if the same value is explicitly assigned to the attribute value as before change.

Prerequisites.

Rails: 5.2
Ruby: 2.5.1

main topic.

Sample User Table Definitions

create_table :users do |t|
  t.string :name
  t.timestamps
end
Enter fullscreen mode Exit fullscreen mode

Defining the user model

class User < ApplicationRecord
end
Enter fullscreen mode Exit fullscreen mode

Create a suitable User model for the sample and display the updated_at

User.first.updated_at
=> Tue, 29 Jan 2019 03:56:08 UTC +00:00
User.first.updated_at.to_i
=> 1548734168
Enter fullscreen mode Exit fullscreen mode

Update the name attribute with the value originally included in the User model.
You can see that the SQL of the UPDATE statement is not executed compared to the changes described below.
You can see that the updated_at of the execution result is 1548734168, which is the same as the update_at before the execution of the update method.

> User.first.update!(name: User.first.name)
  User Load (0.2ms) SELECT "users". * FROM "users" ORDER BY "users"." "id" ASC LIMIT ? id" ASC LIMIT ?  [["LIMIT", 1]]]
  User Load (0.2ms) SELECT "users". * FROM "users" ORDER BY "users"." "id" ASC LIMIT ? id" ASC LIMIT ?  [["LIMIT", 1]]]
   (0.1ms) begin transaction
   (0.0ms) commit transaction
=> true
irb(main):008:0> User.first.updated_at.to_i
=> 1548734168
Enter fullscreen mode Exit fullscreen mode

After verification, we can see that the value of the name attribute is changed, the SQL of the UPDATE statement is executed and the value of updated_at is updated

> User.first.update!(name: User.first.name + "1")
  User Load (0.2ms) SELECT "users". * FROM "users" ORDER BY "users"." "id" ASC LIMIT ? id" ASC LIMIT ?  [["LIMIT", 1]]]
  User Load (0.2ms) SELECT "users". * FROM "users" ORDER BY "users"." "id" ASC LIMIT ? id" ASC LIMIT ?  [["LIMIT", 1]]]
   (0.1ms) begin transaction
  User Update (1.3ms) UPDATE "users" SET "name" = ? , "updated_at" = ? WHERE "users"." id" = ?  [["name", "test 1"], ["updated_at", "2019-02-01 08:27:12.468565"], ["id", 2]]
   (2.9ms) commit transaction
=> true

> User.first.updated_at.to_i
  User Load (0.3ms) SELECT "users". * FROM "users" ORDER BY "users"." "id" ASC LIMIT ? id" ASC LIMIT ?  [["LIMIT", 1]]]
=> 1549009632
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay