質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

MariaDB

MariaDBは、MySQL派生のオープンソースなリレーショナルデータベースシステムです。 また、MySQLとほぼ同じデータベースエンジンに対応しています。

Q&A

解決済

1回答

1614閲覧

railsでcase_sensitive: trueが効かない(mariaDB)

msickpaler

総合スコア14

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

MariaDB

MariaDBは、MySQL派生のオープンソースなリレーショナルデータベースシステムです。 また、MySQLとほぼ同じデータベースエンジンに対応しています。

0グッド

0クリップ

投稿2020/04/29 03:57

編集2020/04/29 12:43

お世話になっております。
railsのcase_sensitiveについて、どうしても分からないため質問させて頂きました。
よろしくお願い致します。

前提・実現したいこと

現在Nuxt.jsとrailsAPIとmariaDBでアプリを作成しています。そこでユーザー登録のためemailにバリデーションをつけたいと思っており、具体的には以下です。

実現したいこと: emailのバリデーションで大文字小文字を区別したい

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

case_sensitive: trueを明記しても大文字小文字の区別がされない。

実際にrspecのエラーメッセージには"Email has already been taken"と書かれており、大文字小文字の区別ができていないことが分かります。

Failures: 1) User users check 'case_sensitive: true' Failure/Error: expect(user).to be_valid expected #<User id: nil, provider: "email", uid: "", name: "test", image: nil, email: "example@example.com", allow_password_change: false, created_at: nil, updated_at: nil> to be valid, but got errors: Email has already been taken, Email has already been taken # ./spec/models/user_spec.rb:79:in `block (3 levels) in <main>' # /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' # /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load' # /usr/local/bundle/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call' # -e:1:in `<main>'

該当のソースコード

######user_spec.rb

it "check 'case_sensitive: true'" do FactoryBot.create(:user, email: "example@example.com") user = FactoryBot.build(:user, email: "Example@example.com") expect(user).to be_valid end

######spec/factories/users.rb

FactoryBot.define do factory :user do name { "test" } sequence(:email) { |n| "example#{n}@example.com" } password { "password" } password_confirmation { "password" } end end

######models/user.rb

# frozen_string_literal: true class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable include DeviseTokenAuth::Concerns::User validates :name, presence: true, length: { maximum: 10 } validates :email, uniqueness: { case_sensitive: true } end

######db/schema.rb

# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_04_11_075604) do create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| t.string "provider", default: "email", null: false t.string "uid", default: "", null: false t.string "name" t.string "image" t.string "email" t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.boolean "allow_password_change", default: false t.datetime "remember_created_at" t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" 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.text "tokens" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false 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 ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true end end

試したこと

  • テスト環境のDBのデータを初期化しました。その後にテストを行ったので、データがロールバックされていない訳ではありません。
  • rails consoleでも大文字小文字の区別がされず、登録できませんでした。
  • rails newでmodelのみを作成したアプリで試したところ、やはりcase_sensitiveが効きませんでした。(DBはsqlite3)

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

ruby 2.6.3
rails 6.0.2.2
devise 4.7.1
devise_token_auth 1.1.3
mariaDB 10.5.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2020/04/29 04:08

データベース構造はどのようになっていますか?
msickpaler

2020/04/29 12:44

気づくのが遅れて申し訳ございません! 初学者ゆえ無知でデータベース構造というものがよくわかっておらず、とりあえずschema.rbを追加しました。 よろしくお願い致します。
guest

回答1

0

自己解決

deviseの設定ファイルに以下の設定がありました。
######config/initializers/devise.rb

config.case_insensitive_keys = [:email] # []にすると区別できる。

デフォルトでemailが設定されているのでこの行をコメントアウトしても意味がなく、空のオブジェクトを渡すとうまく大文字小文字を区別できました。([:name]などのユーザを識別するものが代わりにないと問題が起こりやすいと思います)

投稿2020/05/02 04:14

msickpaler

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問