title: Create random username for Devise user
body: <p>I'm using Devise in a rails 4 app.</p>
  
  <p>Users have a username that is not required on sign up, but I want to assign one randomly to them.</p>
  
  <p>I've made the below controller and made sure that :username is in the strong parameters.</p>
  
  <p>The username is not being saved. Can you help me understand why?</p>
  
  <pre><code>class UsersController &lt; DeviseController
    before_create :set_username
  
    private
      def set_username
        o = [1..9].map { |i| i.to_a }.flatten
        self.username = "user" + (0...10).map { o[rand(o.length)] }.join
      end
  end
  </code></pre>