0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】特定の範囲内で一意のバリデーション

Posted at

実装方法

以下のように記述するとscopeに指定した範囲内で一意であることを検証できます。

project.rb
class Project < ApplicationRecord
  belongs_to :account

  has_many :tasks

  validates :name, presence: true, uniqueness: { scope: :account_id }
end

上記の場合にはnameは同一のaccount_idで一意でなければなりません。つまり、nameaccount_idの組み合わせは一意でなければなりません。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?