I would like to generate random, unique strings matching this pattern: [A-Z]{3}
. How can I achieve this with FactoryGirl or Faker?
我想生成与此模式匹配的随机、惟一的字符串:[A-Z]{3}。我如何与工厂女孩或伪造者达成这一目标?
I thought about FactoryGirl's sequences, but can't make it work.
我想过工厂女孩的序列,但没能成功。
2 个解决方案
#1
8
Try this:
试试这个:
FactoryGirl.define do
sequence :str do |n|
(0..2).map { (65 + rand(26)).chr }.join
end
end
FactoryGirl.generate :str # => "GUW"
#2
4
I've ended up with:
我结束了:
sequence(:code) { ('A'..'Z').to_a.sample(3).join }
#1
8
Try this:
试试这个:
FactoryGirl.define do
sequence :str do |n|
(0..2).map { (65 + rand(26)).chr }.join
end
end
FactoryGirl.generate :str # => "GUW"
#2
4
I've ended up with:
我结束了:
sequence(:code) { ('A'..'Z').to_a.sample(3).join }