With the help of @rich-peck I was able to figure out the solution to this problem after reading the source code of friendly_id. The root of the problem was that friendly_id uses a before_validation to set the slug.
In order to fix my problem, I had to set my model as following.
class User < ActiveRecord::Base
extend FriendlyId
before_validation :set_default_username
friendly_id :username, use: :slugged
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
private
def set_default_username
self.username ||= "user#{User.maximum(:id).next}"
end
def should_generate_new_friendly_id?
slug.blank? || username_changed?
end
end