Search code examples
ruby-on-railsrubyfaker

How to use faker gem to generate fake food names?


I have read up several tutorials, but I am still unsure how to do what I want, therefore I am sorry if it sounds dump. I have an active record called "Sandwitches", which has as attributes: name, price and availability. As I want to generate fake data for it, I am not quite sure how I can achieve something like this, because the name attribute faker can generate stands for names of persons. I would like for instance to generate names for the sandwitches, like "Club Sandwitch" or "Pesto with Feta Cheese". Is there anyway to do this with faker gem ? Or basically could I use any other gem to achieve that?

I apppreciate any help you can provide!


Solution

  • If you take a look at the faker gem here: https://github.com/stympy/faker/blob/master/lib/faker/default/food.rb (note, I am not affiliated w/ faker development) You can see there is a food module. So try this:

    >> Faker::Food.dish
    => "Tiramisù"
    >> Faker::Food.dish
    => "Mushroom Risotto"
    >> Faker::Food.vegetables
    => "Radish"
    

    I also saw a dessert module and some others that looked interesting. Go up one or two trees in github's directory structure to see other options. Faker does a lot of things!

    Edit: Also, your AR table should be "Sandwiches", not "Sandwitches" ;) They're not witches with sand powers, they're food w/ bread ;p

    Another Edit: I don't see an option specifically for sandwiches. But maybe since this is for fake data you can just use the dish option.

    Final Edit I swear: You could "fake" a sandwich with something like:

    breads = ["Brioche", "Rye", "Whole Wheat"] # Add more breads here, https://en.wikipedia.org/wiki/List_of_breads can help
    ingredients = (1..(rand(3)+1)).map{rand > 0.5 ? Faker::Food.ingredient : Faker::Food.vegetables}
    sandwich = "#{ingredients.to_sentence} on #{breads.sample(1).first}"
    

    Which can return results like:

    => "Buckwheat Flour on Rye"
    => "Broccoli and Jicama on Whole Wheat"
    => "Peppers on Rye"
    => "Chia Seeds on Rye"
    => "Pecan Nut and Anchovies on Brioche"
    => "Arugula on Rye"