Rake任务中未初始化的常量Faker :: Restaurant

时间:2018-09-21 18:53:57

标签: ruby-on-rails ruby faker

我已经设置了一个rake任务来填充数据库,尽管一切似乎都准备就绪,但似乎找不到Faker。

Rake任务:

require 'faker'

desc 'Populate db'
task :popdb => [:environment] do 
    puts 'Generating categories...'

    5.times do
        category = Category.create(title: Faker::Restaurant.name)
        puts "Created category \"" + category.title + "\""
        50.times do
            card = Card.create(
                retailer: Faker::Company.name,
                category: category,
                offer_type: Faker::Company.buzzword,
                first_purchase: Faker::Company.industry,
                credit_limit: Faker::Number.between(10, 50) * 10
                )
            puts "Created card \"" + card.title + "\""
        end
    end
end

宝石文件:

  source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'devise', '~> 4.2'
gem 'pg'
gem 'rails', '~> 5.1.6'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'activeadmin'

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'shoulda-matchers', '~> 3.1'
  gem 'faker'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'better_errors'
  gem 'binding_of_caller'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我正在使用最新版本的Faker,并且在测试和开发环境中都包含了gem。我想念什么?谢谢!

2 个答案:

答案 0 :(得分:1)

您实际上在rubygems.org中使用了gem,版本为 1.9.1-2018年7月11日,但 Faker :: Restaurant 已于7月18日添加,可能是可以在rubygems.org的下一版本中获得。

(请参阅https://github.com/stympy/faker/blob/master/doc/restaurant.md

就像上面的评论所建议的那样使用:

gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master'

这将从github而不是rubygems.org下载gem

答案 1 :(得分:0)

注意:

如果遇到uninitialized constant Faker::[some_class]错误,则说明您的gem版本在此处记录的版本背后。为确保您的宝石是此处记录的宝石,请将gemfile中的行更改为:

gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master'

https://github.com/stympy/faker#installing