faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.
MIT License
11.28k stars 3.18k forks source link

NoMethodError (private method `rand' called for 0:Integer): #2995

Closed adenta closed 3 months ago

adenta commented 3 months ago

Describe the bug

When trying to run faker in a model (giving a song a random name) I am encountering the issue: NoMethodError (private methodrand' called for 0:Integer):`

To Reproduce

  1. install latest version of faker
  2. mount the following controller:
    
    def show
    render json: {name: Faker::Creature::Animal.name}
    end

Expected behavior

This should work, but it doesn't

adenta commented 3 months ago

let me do some more digging on my own. Think it has something to do with seeding

adenta commented 3 months ago

Indeed, the following code works:

 def name_prefix
      Faker::Config.random = Random.new(id.to_i)
      [Faker::Verb.ing_form, Faker::Creature::Animal.name].join('_').gsub(/\s+/, '_').downcase
    end

gotta wrap the seed in the random method, which isn't incredibly obvious but an easy fix