ruby-on-railsrubydeviseruby-on-rails-6devise-invitable

invitation_accepted? returns FALSE in after_invitation_accepted CALLBACK


enter image description hereI use devise_invitable (2.0.6)

invitation_accepted? returns FALSE in after_invitation_accepted CALLBACK but when I pause here and check in rails console it gives me true for invitation_accepted?

user.rb

after_invitation_accepted :create_something

def create_something
  create_service unless invitation_sent_at? && !invitation_accepted?  
end

here, invitation_accepted? is false inside this callback but, when I pause here and check in rails console it gives me true

Did I miss anything??


Solution

  • There is comment right above the invitation_accepted? in the source code:

    # Verifies whether a user accepted an invitation (false when user is accepting it)
    def invitation_accepted?
      !accepting_invitation? && invitation_accepted_at.present?
    end
    

    That means invitation_accepted? is not true while the user is accepting the invitation, but only after the user accepted the invitation.

    When looking at what the implementation of accept_invitation! then we see that @accepting_invitation is set to true in the method before the invitation is accepted, but it is not set to false after the record in the database was updated.

    What means only after re-loading the user from the database invitation_accepted? will return true.