Purpose: I want to put 1 because the user_id column is nil
■ Data you want to operate
Notification id: 2, shown_from_at: "2021-01-04 02:32:00", shown_to_at: "2021-01-05 02:32:00", subject: "It's a test", body: "Test test", created_at: "2021-01-04 02:32:55", updated_at: "2021-01-04 07:23:28", important_notification: true, user_id: nil
rails console
Notification.find(2).update(user_id: 1)
user_Overwrite id 1
Notification Load (1.4ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
=> false
See what's happening
rails console
notification = Notification.find(2)
notification.user_id = 1
notification.valid?
notification.errors.full_messages
rails console
The result is as follows.
irb(main):017:0> notification = Notification.find(2)
Notification Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
irb(main):018:0> notification.user_id = 1
irb(main):019:0> notification.valid?
=> false
irb(main):020:0> notification.errors.full_messages
=> ["display period(from)Is an invalid value", "display period(to)Is an invalid value"]
From the above contents, the format defined by "shown_from_at:" 2021-01-04 02:32:00 ", shown_to_at:" 2021-01-05 "is different, so an error may have occurred? Discovered
Since the format is "yyyy/mm/dd HH: MM", the error can be avoided by specifying the correct format when entering the value.
rails console
Notification.find(2).update( shown_from_at: "2021/01/04 02:32",shown_to_at: "2021/01/05 02:32", user_id: 1)
# user_Overwrite id 1
Notification Load (0.3ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.1ms) BEGIN
Notification Update (0.3ms) UPDATE "notifications" SET "shown_from_at" = $1, "shown_to_at" = $2, "user_id" = $3, "updated_at" = $4 WHERE "notifications"."id" = $5 [["shown_from_at", "2021-01-03 17:32:00"], ["shown_to_at", "2021-01-04 17:32:00"], ["user_id", 1], ["updated_at", "2021-01-04 08:24:25.802888"], ["id", 2]]
(0.3ms) COMMIT
=> true
Recommended Posts