環境
- Rails: 5.0.0
- devise: 4.2.0
問題
- Railsとdeviseのバージョンを上げてテストしたところ、ユーザ登録時に確認メールが送信されなくなってしまった。
原因
Userモデルに、after_create
を実装していたのが原因だった。
# models/user.rb:
class User < ActiveRecord::Base
after_create { |record|
hoge
}
end
深く調べていないが、DeviseのConfirmableモジュールのafter_commit
で定義した内容が、after_create
によって上書きされたのかなと思っている。
# devise/lib/devise/models/confirmable.rb:
module Confirmable
extend ActiveSupport::Concern
included do
before_create :generate_confirmation_token, if: :confirmation_required?
after_create :skip_reconfirmation_in_callback!, if: :send_confirmation_notification?
if respond_to?(:after_commit) # ActiveRecord
after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?
対応方法
Userモデル内でafter_create
の代わりにafter_create_commit
を使うようにしたら、確認メールが送信されるようになった。
# models/user.rb:
class User < ActiveRecord::Base
after_create_commit :hoge
end