Q:

In Rails, How To generate a 4 digit ID with FactoryGirl?

I need to generate a 4 digit unique ID using factory girl. I used rand but its not working. It will generate a random number and assign it to all records. How can I do this?
Here's how I used the rand method :
 nyaaid "NYAA/N/WP#{Random.rand(0002..9999)}"

A:

Another alternative is to use String#% to zero-pad your random number to four digits:
'%04d' % Random.rand(2..9999)

PS: 0002 doesn't mean what you probably think it does. The leading zero indicates that that number is in octal (look at 0010 in irb and you'll see 8, not 10). You're getting lucky because 2 is 2 in octal and decimal but you really should use 2..9999 to avoid confusion.

A:

The below code could work if I understand your problem correctly.
[1,1,1,1].map!{|x| (0..9).to_a.sample}.join

