【问题标题】:How to use faker gem to generate fake food names?如何使用 faker gem 生成假食品名称?
【发布时间】:2019-06-18 14:23:24
【问题描述】:

我已经阅读了几个教程,但我仍然不确定如何做我想做的事,因此如果听起来很垃圾,我很抱歉。我有一个名为“Sandwitches”的活动记录,它具有以下属性:namepriceavailability。由于我想为它生成假数据,我不太确定如何实现这样的目标,因为 name 属性 faker 可以生成人名的代表。例如,我想为三明治生成名称,例如“Club Sandwitch”或“Pesto with Feta Cheese”。有没有办法用 faker gem 做到这一点?或者基本上我可以使用任何其他宝石来实现这一目标吗?

感谢您提供的任何帮助!

【问题讨论】:

    标签: ruby-on-rails ruby faker


    【解决方案1】:

    如果您在这里查看 faker gem:https://github.com/stympy/faker/blob/master/lib/faker/default/food.rb(注意,我不隶属于 faker 开发)您可以看到有一个 food 模块。所以试试这个:

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

    我还看到了一个甜点模块和其他一些看起来很有趣的模块。在 github 的目录结构中上一两棵树以查看其他选项。 Faker 做了很多事情!

    编辑:另外,你的 AR 桌子应该是“三明治”,而不是“三明治”;)他们不是具有沙力的女巫,他们是带面包的食物;p

    另一个编辑:我没有看到一个选项专门用于三明治。但也许因为这是假数据,你可以只使用盘选项。

    我发誓最后的编辑:你可以用类似的东西“伪造”一个三明治:

    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}"
    

    可以返回如下结果:

    => "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"
    

    【讨论】:

      【解决方案2】:

      除了@nzifnab answer,您还可以干燥您的规格并组织您自己的定制假货(基于@nzifnab 代码)

      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
      

      【讨论】:

      • 我认为是expect(...).not_to,而不是to_not?除非两者都有效?我一直在 rspec 中使用not_to
      【解决方案3】:

      型号名称 = 项目。

      db 列名称 = (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)

      在 /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
      

      【讨论】:

        【解决方案4】:

        您可以访问以下链接以获取 faker gem 的文档。 https://github.com/stympy/faker

        例如,

        5.times.do
         name = Faker::Name.first_name
         price = Faker::Name.price
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-28
          • 1970-01-01
          • 2014-03-12
          • 1970-01-01
          • 2018-09-08
          • 1970-01-01
          • 2013-07-18
          相关资源
          最近更新 更多