ソースを参照

Add user validation

Maarten van den Berg 8 年 前
コミット
7680dfb081
共有1 個のファイルを変更した12 個の追加0 個の削除を含む
  1. 12 0
      app/models/user.rb

+ 12 - 0
app/models/user.rb

@@ -1,4 +1,16 @@
1 1
 class User < ApplicationRecord
2 2
   has_secure_password
3 3
   belongs_to :person
4
+
5
+  validates :person, presence: true
6
+  validates :email, uniqueness: true
7
+
8
+  before_validation :email_same_as_person
9
+
10
+  private
11
+  def email_same_as_person
12
+    if self.person and self.email != self.person.email
13
+      errors.add(:email, "must be the same as associated person's email")
14
+    end
15
+  end
4 16
 end