bcryptを使ってUserをActiveJobを用いて作成したらエラー(Unknown column 'users.password' in 'where clause')が起きる

前提

railsを使ってアプリケーションを開発しています。
https://qiita.com/kazama1209/items/a08fe0d25384b4a6ea99
こちらの記事を参考にして、
①スプレッドシートの値を読み取る
②その値を元にUserモデルを作成する
③Userがマイページ(viewファイル)から自分の情報を読み取ることができる

というログイン機能つきのマイページを作成しようとしています。

bcryptのhas_secure_passwordを用いて、ハッシュ化されたパスワードを利用しようと考えています。

以下、主なコードです。

schema.rb

ActiveRecord::Schema.define(version: 2022_09_30_122620) do create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "user_name" t.integer "chip" t.integer "winning_streak" t.integer "point" t.integer "a_rule_game" t.integer "a_rule_first_place" t.integer "a_rule_second_place" t.integer "a_rule_third_place" t.integer "a_rule_rating" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "b_rule_game" t.integer "b_rule_first_place" t.integer "b_rule_second_place" t.integer "b_rule_third_place" t.integer "b_rule_rating" t.string "password_digest" end end

user.rb

class User < ApplicationRecord has_secure_password end

spreadsheets.rb

require "google/apis/sheets_v4" module Google class Spreadsheets def initialize @service = Google::Apis::SheetsV4::SheetsService.new @service.authorization = authorize end # 認証 def authorize json_key = JSON.generate( private_key: ENV['GOOGLE_PRIVATE_KEY'], client_email: ENV['GOOGLE_CLIENT_EMAIL'] ) json_key_io = StringIO.new(json_key) authorizer = Google::Auth::ServiceAccountCredentials.make_creds( json_key_io: json_key_io, scope: ["https://www.googleapis.com/auth/spreadsheets"] ) authorizer.fetch_access_token! authorizer end # 指定されたスプレッドシートIDとレンジ(範囲)から値を取得 def values(spreadsheet_id, ragne) @service.get_spreadsheet_values(spreadsheet_id, ragne) end end end

spreadsheets_import_job.rb

class SpreadsheetsImportJob < ApplicationJob queue_as :default # 行の構造を定義 Row = Struct.new( :user_name, :password, :chip, :winning_streak, :point, :a_rule_game, :a_rule_first_place, :a_rule_second_place, :a_rule_third_place, :a_rule_rating, :b_rule_game, :b_rule_first_place, :b_rule_second_place, :b_rule_third_place, :b_rule_rating ) def perform(spreadsheet_id, range) res = google_spreadsheet_service.values(spreadsheet_id, range) return if res.values.empty? # 値が空だった場合はここで終了 res.values.drop(1).each do |row_data| # 1行目はヘッダーなので削除 row = Row.new(*row_data) attributes = row.to_h.slice( :user_name, :password, :chip, :winning_streak, :point, :a_rule_game, :a_rule_first_place, :a_rule_second_place, :a_rule_third_place, :a_rule_rating, :b_rule_game, :b_rule_first_place, :b_rule_second_place, :b_rule_third_place, :b_rule_rating ) # 重複するデータを作成したくないのでfind_or_initialize_byを使用 user = User.find_or_initialize_by(attributes) user.save end end private def google_spreadsheet_service @google_spreadsheet_service ||= Google::Spreadsheets.new end end

sessions_controller.rb

class SessionsController < ApplicationController def new; end def create SpreadsheetsImportJob.perform_now("1MDPFwFVab9_xnbPcMV2eMCd6PwVF_to6FMMytd54wwA", ["データ読取用!A:O"]) user = User.find_by(user_name: params[:session][:user_name]) if user&.authenticate(params[:session][:password]) log_in user redirect_to user else flash.now[:danger] = '名前かパスワードが正しくありません!' render 'new' end end def destroy log_out redirect_to 'new' end end

発生している問題・エラーメッセージ

sessions_controllerのcreateメソッドを利用するときにSpreadsheetsImportJob.perform_now("1MDPFwFVab9_xnbPcMV2eMCd6PwVF_to6FMMytd54wwA", ["データ読取用!A:O"])
というactive jobを使いますが、そのjobを実行する際にusersテーブルにpasswordカラムが存在しないというエラーが発生します。
イメージ説明

ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'users.password' in 'where clause')

active jobでUserモデルが上手く作動せずにエラーが起こっているのかと思うのですが、調べてみても解決策が浮かばないため質問させていただきました。

試したこと

has_secure_passwordがUserモデル内で作動しておらず、passwordカラムが利用できないのかと思い、コンソール上でデータを作成しましたが、
Alice = User.new(user_name: "Alice", password: "2345678")
でユーザーを作成し、
Alice.passwordとしたところ2345678、
Alice.password_digestとしたところハッシュ化された文字列が出てきました。

補足情報(FW/ツールのバージョンなど)

ruby 2.7.5
rails 6.1.6
bcrypt 3.1.7

コメントを投稿

0 コメント