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
Defining the user model
class User < ApplicationRecord
end
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
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
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
Top comments (0)