Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

0

Just Ruby; no rails or whatever:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda