Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
How can I generate sequence values of mixed letter characters and number digits in FactoryBot?

I need to generate sequences of labels that look like "ABC1" and "XYZ9" -- always three letters followed by a single digit. I want both parts of the label to increment logically, and always be the exact format and length.

I started with:

sequence(:code) { |n| "ABC#{n}" }

That was fine, but after the ninth label (ABC9) it went to (ABC10), violating my formatting, and only producing 10 (or 9?) values.

A quick fix of using the sequence for the letters and hard-coding one digit makes my rollover problem not occur for 26^3 => 17,576 instances, which might be okay. But, I really want some variability in the digit as well. A random digit would probably be good enough...but I want all the available values.

I thought about a few somewhat kludgy ways of building the string from one or two sequences to get comprehensive values always matching the format, but they seemed clunky.

Is there an elegant way in FactoryBot to build a sequenced string with a fixed-width letter segment followed by a fixed-width numeric segment that produces all possible values?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Just Ruby; no rails or whatever:

code = "AAA0"
20.times{ puts code.succ! }
over 4 years ago · Santiago Trujillo Report

0

As it turns out, the solution in a factory is sublimely simple and graceful:

sequence(:code, 'AAA1')

This produces the following sequence:

FactoryBot.build :code

 => #<Code code: "AAA1">
 => #<Code code: "AAA2">
 => #<Code code: "AAA3">
 => #<Code code: "AAA4">
 => #<Code code: "AAA5">
 => #<Code code: "AAA6">
 => #<Code code: "AAA7">
 => #<Code code: "AAA8">
 => #<Code code: "AAA9">
 => #<Code code: "AAB0">
 => #<Code code: "AAB1">
 => #<Code code: "AAB2">
... four hours later ...
 => #<Code code: "ZZY7">
 => #<Code code: "ZZY8">
 => #<Code code: "ZZY9">
 => #<Code code: "ZZZ0">
 => #<Code code: "ZZZ1">
 => #<Code code: "ZZZ2">
 => #<Code code: "ZZZ3">
 => #<Code code: "ZZZ4">
 => #<Code code: "ZZZ5">
 => #<Code code: "ZZZ6">
 => #<Code code: "ZZZ7">
 => #<Code code: "ZZZ8">
 => #<Code code: "ZZZ9">
 => #<Code code: "AAAA0">
 => #<Code code: "AAAA1">

For my needs, I'm not concerned about the total rollover after 175,760 values.

It's also worth noting that you can start each segment wherever you like. This works just as well:

sequence(:code, 'ABC9')


FactoryBot.build :code

 => #<Code code: "ABC9">
 => #<Code code: "ABD0">
 => #<Code code: "ABD1">
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!