我的代码摘录在哪里(注释显示了它在测试期间输出的内容)以及我决定使用的内容
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :confirmable,
:async, :authentication_keys => [:email]
after_update :save_old_email_in_history, if: :saved_change_to_email? # XXX: check the rails documentation for saved_change_to_ATTRIBUTE?
def save_old_email_in_history
# https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html
#
# puts(
# {
# previous_changes: previous_changes,
# email_was: email_was,
# email: email,
# saved_change_to_email: saved_change_to_email,
# email_before_last_save: email_before_last_save,
# }.to_s
# )
# In test outputs
# {
# :previous_changes=>{
# "email"=>["idella@koelpin-rodriguez.com", "wanita_jakubowski@gottlieb.com"],
# "confirmed_at"=>[Mon, 01 Nov 2021 13:29:47 EDT -04:00, Mon, 01 Nov 2021 13:30:02 EDT -04:00],
# "unconfirmed_email"=>["wanita_jakubowski@gottlieb.com", nil],
# "updated_at"=>[Mon, 01 Nov 2021 13:29:57 EDT -04:00, Mon, 01 Nov 2021 13:30:02 EDT -04:00]
# },
# :email_was=>"wanita_jakubowski@gottlieb.com",
# :email=>"wanita_jakubowski@gottlieb.com",
# :saved_change_to_email=>["idella@koelpin-rodriguez.com", "wanita_jakubowski@gottlieb.com"],
# :email_before_last_save=>"idella@koelpin-rodriguez.com"
# }
UserEmailHistoryItem.create!(user: self, email: email_before_last_save)
end