LoginSignup
5
3

More than 5 years have passed since last update.

【Rails】カラムの組み合わせに一意性を持たせる

Last updated at Posted at 2017-07-04

対象者

複数カラムをセットにして複合的にuniquenessバリデーションをかけたい人

環境

Ruby 2.4.1
Rails 5.0.2

実装

user.rb
class User < ActiveRecord::Base
  validates :name,  uniquness: { scope: [:email, :pass]  }
end

name: 'tanaka',email: 't.com', pass: 'tktk'
というユーザーが既にいる場合Validation結果は以下になる

OK
name: 'tanaka',email: 't.com', pass: 'aaaa'
NG
name: 'tanaka',email: 't.com', pass: 'tktk'

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3