title: Rails numerate records
body: <p>I have a list of records with <code>number</code> attribute.</p>
  
  <ul>
  <li>Invoice(user_id: 1, number: nil)</li>
  <li>Invoice(user_id: 1, number: nil)</li>
  <li>Invoice(user_id: 2, number: nil)</li>
  <li>Invoice(user_id: 3, number: nil)</li>
  </ul>
  
  <p>My desired result is:</p>
  
  <ul>
  <li>Invoice(user_id: 1, number: 001)</li>
  <li>Invoice(user_id: 1, number: 002)</li>
  <li>Invoice(user_id: 2, number: 001)</li>
  <li>Invoice(user_id: 2, number: 002)</li>
  </ul>
  
  <p>Tried:</p>
  
  <pre><code>    Invoice.all.each do |i|
       i.number = "#{sprintf '%03d', i.user.invoices.count + 1}"
       i.save
      end
  </code></pre>
  
  <p>But got the current result:</p>
  
  <ul>
  <li>Invoice(user_id: 1, number: 002)</li>
  <li>Invoice(user_id: 1, number: 002)</li>
  <li>Invoice(user_id: 2, number: 003)</li>
  <li>Invoice(user_id: 2, number: 003)</li>
  </ul>
  
  <p>How do I get the desired result? Thanks</p>