質問編集履歴

1

カラムを追加して原因を調査

2022/12/14 00:19

投稿

tkd00
tkd00

スコア6

test CHANGED
File without changes
test CHANGED
@@ -61,4 +61,57 @@
61
61
  ruby 2.5.3
62
62
  Rails 5.2.8.1
63
63
 
64
+ ### 2022/12/14 追記
65
+ integer型のカラム id100を追加
66
+ db/schema.rb
67
+ ```ruby
68
+ ActiveRecord::Schema.define(version: 2022_12_14_000216) do
64
69
 
70
+ create_table "users", force: :cascade do |t|
71
+ t.string "email", default: "", null: false
72
+ t.string "encrypted_password", default: "", null: false
73
+ t.string "reset_password_token"
74
+ t.datetime "reset_password_sent_at"
75
+ t.datetime "remember_created_at"
76
+ t.datetime "created_at", null: false
77
+ t.datetime "updated_at", null: false
78
+ t.integer "id100"
79
+ t.index ["email"], name: "index_users_on_email", unique: true
80
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
81
+ end
82
+
83
+ end
84
+ ```
85
+
86
+ app/controllers/users/registrations_controller.rbを変更
87
+ ```ruby
88
+ class Users::RegistrationsController < Devise::RegistrationsController
89
+ # before_action :configure_sign_up_params, only: [:create]
90
+ # before_action :configure_account_update_params, only: [:update]
91
+
92
+ # GET /resource/sign_up
93
+ # def new
94
+ # super
95
+ # end
96
+
97
+ # POST /resource
98
+ def create
99
+ super {|user|
100
+ user.update(id100: user.id*100)
101
+ }
102
+ end
103
+ end
104
+ ```
105
+ rails serverから新規登録を行うと、id100にuser.id*100の値が書き込まれる。
106
+ rails consoleで確認すると
107
+ ```
108
+ user = User.find(102)
109
+ user.id100
110
+ => 10200
111
+ ```
112
+ rails consoleでcreateを実行すると、id100はnilとなる。
113
+ ```
114
+ user = User.create(email:"test4@example.com", password:"test0101")
115
+ user.id100
116
+ => nil
117
+ ```