方法:genrate random code
class User
  #use after create for setting the username
  after_create :set_username
  private
    def set_username
       self.username = "user-#{ SecureRandom.hex(10)}"
    end
end
class User
  #use before update for setting the username
  before_update :set_username
  private
    def set_username
       self.username = "user-#{ SecureRandom.hex(10)}"
    end
end

上面两个方法在devise里面是等价的,但是不可以用before_create,因为注册成功会update一下,导致生成的user_name在update时候没有了。 来源1

resumehack用来生成user_code的方法如下model user

before_update :create_user_code

private

  def create_user_code
    if self.user_code.blank?
       self.user_code = SecureRandom.hex
       self.save!
    end
  end

Simple invitation access code using Rails 4 & Devise How to Add an Invitation Code to Devise and Rails Ruby On Rails User SignUp Email Confirmation Tutorial