Padding a number with zeros
puts 1.to_s.rjust(3, "0")
#=> 001
puts 10.to_s.rjust(3, "0")
#=> 010
puts 100.to_s.rjust(3, "0")
#=> 100
The above code would convert your user.id into a string, then String.rjust() method would consider its length and prefix appropriate number of zeros.
String#rjust
:
user.id
#⇒ 5
user.id.to_s.rjust(3, '0')
#⇒ "005"
You better use string format.
"%03d" % 1 #=> "001"
"%03d" % 10 #=> "010"
"%03d" % 100 #=> "100"
"%03d" % user.id # => what you want