I can't overwrite information on columns in rails console ...

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

I can't overwrite information on columns in rails console ...
I can't install rails on my mac
Adding columns in Rails
[Ruby] I can't install puma on Mac [Rails, etc.]
I want to define a function in Rails Console
I can't log in on Heroku when I introduce Basic authentication.
Where I was interested in Progate's Ruby on Rails course [params]
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
I want to add devise in Rails, but I can't bundle install
[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
I summarized the flow until implementing simple_calendar in Ruby on Rails.
How to debug the processing in the Ruby on Rails model only on the console
Ruby on Rails in Visual Studio Codespaces
Beginners create portfolio in Ruby on Rails
Where I got stuck in today's "rails tutorial" (2020/10/08)
How to add / remove Ruby on Rails columns
I tried to organize the session in Rails
Recommendation of Service class in Ruby on Rails
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
Rails new in Ruby on Rails ~ Memorandum until deployment 1
Where I got stuck in today's "rails tutorial" (2020/10/04)
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to install Bootstrap in Rails
ActiveModel :: MissingAttributeError (can't write unknown attribute 〇〇) in Rails
Where I got stuck in today's "rails tutorial" (2020/10/07)
I made a portfolio with Ruby On Rails
[Rails / Uniqueness constraint] How to check model validation on the console / Uniqueness constraint for multiple columns