title: Rails 5 - After registration, assign role and remove default role
body: <p>I'm using Rails 5.2.3 with Devise and Rolify. On registration I am assigning a default role to all users, called <code>pending</code>. This works great but in certain situations I want to pass a parameter to the registration controller or user model to allow one of the following situations (doesn't matter which one, I'm fine with either):</p>
  
  <ul>
  <li>The model overrides the default role and creates the new role instead</li>
  <li>The registration happens as it usually would and afterwards a new role is added (<code>client</code>) and the default role (<code>pending</code>) is removed.</li>
  </ul>
  
  <p>This is my user model:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    rolify
    has_one_attached :avatar
    has_many :bookings, :class_name =&gt; 'Booking', :foreign_key =&gt; :user_id
    has_many :clients, :class_name =&gt; 'Booking', :foreign_key =&gt; :client_id
    before_create :set_username
    after_create :assign_default_role, :send_registration_mail
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable
  
    def assign_default_role
      # Assign the default user role after the user is created.
      add_role(:pending) if self.roles.blank?
    end
  
    def set_username
      # Generate a unique username before creating the user.
      self.username = "#{self.email[/^[^@]+/]}-#{SecureRandom.hex(1)}"
    end
  
    def send_registration_mail
      # Use a delayed job to send the registration email.
      SendRegistrationEmailJob.set(wait: 20.seconds).perform_later(self)
    end
  end
  </code></pre>
  
  <p>Here's my application controller:</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    before_action :configure_permitted_parameters, :store_location, if: :devise_controller?
  
    def record_activity(note)
      @activity = Activity_Log.new
      @activity.user = current_user
      @activity.note = note
      @activity.browser = request.env['HTTP_USER_AGENT']
      @activity.ip_address = request.env['REMOTE_ADDR']
      @activity.controller = controller_name 
      @activity.action = action_name 
      @activity.params = params.inspect
      @activity.save
    end
  
    # Store login redirect location if it exists
    def store_location
      session[:passthru] = params[:passthru] if params[:passthru]
    end
  
    # Redirect to the desired location or default
    def after_sign_in_path_for(resource_name)
      if session[:passthru]
        session.delete(:passthru)
      else
        super
      end
    end
  
    def after_sign_up_path_for(resource_name)
      if session[:passthru]
        if params[:role] = 'client'
          user.add_role (:client)
          pending_role = ActiveRecord::Base.connection.execute("SELECT id from roles where name = pending LIMIT 1").first
          sqlRemovePendingRole = "DELETE FROM users_roles WHERE user_id = #{ current_user.id } AND role_id = #{ pending_role[0] };"
          ActiveRecord::Base.connection.execute(sqlRemovePendingRole)
        end
        session.delete(:passthru)
      else
        super
      end
    end
  
    protected
  
    def configure_permitted_parameters
      # Permit the `mobile_number` parameter along with the other
      # Sign up parameters.
      devise_parameter_sanitizer.permit(:sign_up, keys: [:mobile_number])
      # Permit the `passthru` parameter along with the other
      # Sign in parameters.
      devise_parameter_sanitizer.permit(:sign_in, keys: [:passthru, :role])
    end
  end
  </code></pre>
  
  <p>This is my view where I'm trying to do the registration when passing the role:</p>
  
  <pre><code>    &lt;% if user_signed_in? %&gt;
        Put booking form here
  
      &lt;% else %&gt;
        Please &lt;%= link_to "log in", new_user_session_path(:passthru =&gt; user_booking_path(@user.username)) %&gt; to book &lt;%= @user.firstname %&gt; &lt;%= @user.surname%&gt;.&lt;br /&gt;
        Don't have an account yet? &lt;%= link_to "Register", new_user_registration_path(:passthru =&gt; user_booking_path(@user.username), :role =&gt; "client") %&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>I tried passing variables from the controller to the model (I know I shouldn't) to try to override the default role but that didn't work so I tried the code above to change the role after registration (<code>after_sign_up_path_for</code> action). Anyone able to point me in the right direction?</p>