Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

対象者

複数カラムをセットにして複合的に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'

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away