Reputation: 413
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!
Upvotes: 2
Views: 4069
Reputation: 1520
In addition to the @nzifnab answer you can also dry your specs and organize your own custom fakes (based on @nzifnab code)
spec/support/faker/sandwich.rb
module Faker
class Sandwich
class << self
def title
"#{ingredients.to_sentence} on #{breads.sample(1).first}"
end
def breads
["Brioche", "Rye", "Whole Wheat"] # Add more breads here, https://en.wikipedia.org/wiki/List_of_breads can help
end
def ingredients
(1..(rand(3)+1)).map{rand > 0.5 ? Faker::Food.ingredient : Faker::Food.vegetables}
end
end
end
end
spec/models/sandwiches_spec.rb
describe 'Sandwiches' do
it 'contains name' do
# some checks...
expect(Faker::Sandwich.title).not_to be_empty
end
end
Upvotes: 6
Reputation: 308
model name = Item.
db column name = (name,price,category_id,image_id,short_description,long_description,is_active,preparation_time,calorie_count, meal_type_id,cuisine_id,spicy_level,is_new,is_bestseller)
in /seed.rb
100.times do
Item.create([{
name:Faker::Food.dish,
price:Faker::Number.positive(5, 30),
category_id:Faker::Number.positive(1, 10),
image_id:Faker::Number.positive(1, 20),
short_description:Faker::Lorem.words(rand(2..5)).join(' '),
long_description:Faker::Lorem.words(rand(2..10)).join(' '),
is_active:Faker::Boolean.boolean,
preparation_time:Faker::Number.positive(10, 90),
serves:Faker::Number.between(1, 3),
calorie_count:Faker::Number.between(20, 500),
meal_type_id:Faker::Number.positive(1, 4),
cuisine_id:Faker::Number.positive(1, 10),
spicy_level:Faker::Number.between(1, 3),
is_new:Faker::Boolean.boolean,
is_bestseller:Faker::Boolean.boolean
}])
end
and then in terminal
rake db:seed
Upvotes: 1
Reputation: 623
You can visit below link for documentation of faker
gem.
https://github.com/stympy/faker
For Example,
5.times.do
name = Faker::Name.first_name
price = Faker::Name.price
end
Upvotes: 0
Reputation: 16092
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"
Upvotes: 6