user_idをnilから1にする
また忘れたので書いておく。゚(゚´ω`゚)゚。
最初の状態
irb(main):006:0* @play = Play.first Play Load (0.3ms) SELECT "plays".* FROM "plays" ORDER BY "plays"."id" ASC LIMIT ? [["LIMIT", 1]] => #<Play id: 1, title: "もももショー", description: "もももによるももものためのショー", director: "ももも", created_at: "2017-12-25 09:26:28", updated_at: "2017-12-25 09:26:28", user_id: nil>
@playのuser_idに1を入れる
irb(main):007:0> @play.user_id = 1 => 1
@playのuser_idに1が入る
irb(main):008:0> @play => #<Play id: 1, title: "もももショー", description: "もももによるももものためのショー", director: "ももも", created_at: "2017-12-25 09:26:28", updated_at: "2017-12-25 09:26:28", user_id: 1>
が、Playテーブルには反映していないので、
irb(main):009:0> Play.first Play Load (0.1ms) SELECT "plays".* FROM "plays" ORDER BY "plays"."id" ASC LIMIT ? [["LIMIT", 1]] => #<Play id: 1, title: "もももショー", description: "もももによるももものためのショー", director: "ももも", created_at: "2017-12-25 09:26:28", updated_at: "2017-12-25 09:26:28", user_id: nil>
というわけで、.saveでテーブルに反映
irb(main):010:0> @play.save (0.1ms) begin transaction User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] SQL (0.4ms) UPDATE "plays" SET "updated_at" = ?, "user_id" = ? WHERE "plays"."id" = ? [["updated_at", "2017-12-26 04:52:08.958645"], ["user_id", 1], ["id", 1]] (0.8ms) commit transaction => true
おめでとう!テーブルにもuser_id = 1が反映したよ!
irb(main):011:0> Play.first Play Load (0.2ms) SELECT "plays".* FROM "plays" ORDER BY "plays"."id" ASC LIMIT ? [["LIMIT", 1]] => #<Play id: 1, title: "もももショー", description: "もももによるももものためのショー", director: "ももも", created_at: "2017-12-25 09:26:28", updated_at: "2017-12-26 04:52:08", user_id: 1>
デスヨ!覚えとけよ!自分!! 初歩も初歩すぎてまじで泣ける。