Просмотр исходного кода

Update User email if Person changes email

Maarten van den Berg лет назад: 8
Родитель
Сommit
5b3df5b45d
1 измененных файлов с 8 добавлено и 0 удалено
  1. 8 0
      app/models/person.rb

+ 8 - 0
app/models/person.rb

1
 class Person < ApplicationRecord
1
 class Person < ApplicationRecord
2
+  has_one :user
3
+
2
   validates :email, presence: true, uniqueness: true
4
   validates :email, presence: true, uniqueness: true
3
   validates :first_name, presence: true
5
   validates :first_name, presence: true
4
   validates :last_name, presence: true
6
   validates :last_name, presence: true
7
   validate :birth_date_cannot_be_in_future
9
   validate :birth_date_cannot_be_in_future
8
 
10
 
9
   before_validation :not_admin_if_nil
11
   before_validation :not_admin_if_nil
12
+  before_save :update_user_email, if: :email_changed?
10
 
13
 
14
+  private
11
   def birth_date_cannot_be_in_future
15
   def birth_date_cannot_be_in_future
12
     if self.birth_date > Date.today
16
     if self.birth_date > Date.today
13
       errors.add(:birth_date, "can't be in the future.")
17
       errors.add(:birth_date, "can't be in the future.")
17
   def not_admin_if_nil
21
   def not_admin_if_nil
18
     self.is_admin ||= false
22
     self.is_admin ||= false
19
   end
23
   end
24
+
25
+  def update_user_email
26
+    self.user.update!(email: self.email)
27
+  end
20
 end
28
 end