Search code examples
ruby-on-railsrubyruby-on-rails-4faker

Faker gem: How to set max. length / range for resp. Company.name / Number.number?


I use the Faker gem for seeding certain data. How can I set the max. length for a fake Company.name, and how can I set the range for a fake number?

name  = Faker::Company.name

Here I would like to include the maximum length, since name has a model restriction for max. 40 characters.

code_id = Faker::Number.number

For code_id I would like a range from 1 to 50. I tried code_id = Faker::Number.number(from=1, to=50) but that seems incorrect as on seeding it produced the following error:

ArgumentError: wrong number of arguments (2 for 1)
/usr/local/rvm/gems/ruby-2.1.5/gems/faker-1.4.3/lib/faker/number.rb:4:in 'number'

How should I adjust Faker to my needs?


Solution

  • For the name you can just cut off the extra parts of the generated one (you don't care about half-finished words there, do you?)

    name = Faker::Company.name[0..40]
    

    And for the number you can use Faker::Number.between or use core ruby rand directly.

    rand(1..50)