如何使用faker gem生成假的食物名称?
How to use faker gem to generate fake food names?
我已经阅读了几个教程,但我仍然不确定如何做我想做的,所以如果听起来很垃圾,我很抱歉。我有一个名为 "Sandwitches" 的活动记录,它具有以下属性:name、price 和 availability.因为我想为它生成假数据,所以我不太确定如何实现这样的东西,因为 name 属性 faker 可以生成代表人名。例如,我想为三明治生成名称,例如 "Club Sandwitch" 或 "Pesto with Feta Cheese"。有没有办法用 faker gem 来做这个?或者基本上我可以使用任何其他 gem 来实现吗?
感谢您提供的任何帮助!
如果你看看这里的 faker gem:https://github.com/stympy/faker/blob/master/lib/faker/default/food.rb(注意,我不隶属于 faker 开发)你可以看到有一个食物模块。所以试试这个:
>> Faker::Food.dish
=> "Tiramisù"
>> Faker::Food.dish
=> "Mushroom Risotto"
>> Faker::Food.vegetables
=> "Radish"
我还看到了甜点模块和其他一些看起来很有趣的模块。在 github 的目录结构中向上移动一两棵树以查看其他选项。 Faker 做了很多事情!
编辑:此外,您的 AR table 应该是 "Sandwiches",而不是 "Sandwitches" ;) 他们不是拥有沙之力的女巫,他们是带面包的食物 ;p
另一个编辑:我没有看到特别 用于三明治的选项。但也许因为这是针对虚假数据的,所以您可以只使用 dish 选项。
最终编辑 我发誓:你可以 "fake" 一个三明治,里面有这样的东西:
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}"
哪个可以 return 结果如下:
=> "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"
除了 you can also dry your specs and organize your own custom fakes (based on 代码)
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
您可以访问下方 link 以获取 faker
gem 的文档。
https://github.com/stympy/faker
例如,
5.times.do
name = Faker::Name.first_name
price = Faker::Name.price
end
模型名称=项目。
db 列名 = (名称,价格,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)
在/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
然后在终端
rake db:seed
我已经阅读了几个教程,但我仍然不确定如何做我想做的,所以如果听起来很垃圾,我很抱歉。我有一个名为 "Sandwitches" 的活动记录,它具有以下属性:name、price 和 availability.因为我想为它生成假数据,所以我不太确定如何实现这样的东西,因为 name 属性 faker 可以生成代表人名。例如,我想为三明治生成名称,例如 "Club Sandwitch" 或 "Pesto with Feta Cheese"。有没有办法用 faker gem 来做这个?或者基本上我可以使用任何其他 gem 来实现吗?
感谢您提供的任何帮助!
如果你看看这里的 faker gem:https://github.com/stympy/faker/blob/master/lib/faker/default/food.rb(注意,我不隶属于 faker 开发)你可以看到有一个食物模块。所以试试这个:
>> Faker::Food.dish
=> "Tiramisù"
>> Faker::Food.dish
=> "Mushroom Risotto"
>> Faker::Food.vegetables
=> "Radish"
我还看到了甜点模块和其他一些看起来很有趣的模块。在 github 的目录结构中向上移动一两棵树以查看其他选项。 Faker 做了很多事情!
编辑:此外,您的 AR table 应该是 "Sandwiches",而不是 "Sandwitches" ;) 他们不是拥有沙之力的女巫,他们是带面包的食物 ;p
另一个编辑:我没有看到特别 用于三明治的选项。但也许因为这是针对虚假数据的,所以您可以只使用 dish 选项。
最终编辑 我发誓:你可以 "fake" 一个三明治,里面有这样的东西:
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}"
哪个可以 return 结果如下:
=> "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"
除了
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
您可以访问下方 link 以获取 faker
gem 的文档。
https://github.com/stympy/faker
例如,
5.times.do
name = Faker::Name.first_name
price = Faker::Name.price
end
模型名称=项目。
db 列名 = (名称,价格,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)
在/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
然后在终端
rake db:seed