やりたいこと
自作アプリ内に追加したcreated_atのようなdatetime型のカラムに値を入れたいのですが、うまいこと行きません。
現状
deviseを用いたユーザー登録の仕様で、いくつか追加したカラムがあります。その中で、”received_at”というカラムがあるのですが、その第1段階で、ユーザー作成の際にその時点での時間を入れたいのですがうまくいきません。下にコードを書くのでどこがいけないのか教えていただきたいです。
Users_controller.rb
class UserController < xxx
before_action :authentificate_user!
def create
@user = User.new(user_params)
@user.received_at = Time.now
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url, notice: "Please check your email to activate your account."
else
render 'new'
end
end
end
大雑把に言うとこういう書き方をしているのですが、この @user.received_at = Time.now
の箇所でイメージとしては、created_atのようにその時点での時刻を入れたいのですが、コマンドプロンプトで確認すると、ユーザー作成なんかは出来ているのですが、received_at = nil
になってしまいます。
何か良いアドバイスが有れば、よろしくお願いします。
schema.rb
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.date "birthday"
t.datetime "received_at"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end
deviseの基本的なものがほとんどですが、いくつかカラムを追加しています。
追記2
ユーザー登録時のコマンドプロンプト
この最後の received=at =nil
が気になってます。
Started POST "/users" for ::1 at 2022-05-23 20:55:00 +0900
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"ささ", "birthday"=>"2022-05-03", "email"=>"xxxxxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"アカウント登録"}
TRANSACTION (0.1ms) BEGIN
User Exists? (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "xxxxxxx"], ["LIMIT", 1]]
User Create (0.8ms) INSERT INTO "users" ("email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "confirmation_token", "confirmed_at", "confirmation_sent_at", "unconfirmed_email", "failed_attempts", "unlock_token", "locked_at", "created_at", "updated_at", "name", "birthday", "received_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22) RETURNING "id" [["email", "xxxxxxx"], ["encrypted_password", "[FILTERED]"], ["reset_password_token", "[FILTERED]"], ["reset_password_sent_at", "[FILTERED]"], ["remember_created_at", nil], ["sign_in_count", 0], ["current_sign_in_at", nil], ["last_sign_in_at", nil], ["current_sign_in_ip", nil], ["last_sign_in_ip", nil], ["confirmation_token", "[FILTERED]"], ["confirmed_at", nil], ["confirmation_sent_at", "2022-05-23 20:55:00.331651"], ["unconfirmed_email", nil], ["failed_attempts", 0], ["unlock_token", "[FILTERED]"], ["locked_at", nil], ["created_at", "2022-05-23 20:55:00.331554"], ["updated_at", "2022-05-23 20:55:00.331554"], ["name", "ささ"], ["birthday", "2022-05-03"], ["received_at", nil]]
@user.received_at = Time.now
からif @user.save
の間では@user
には一切触れていないのでしょうか。触れていないのであれば、実は変更が保存されていなかったとか、変更がアプリに反映されていなかったとか、そういう類の原因な可能性はないか、など考えています。