NameError:未初始化的常量Faker

我试图在Rails 4中为我的数据库运行一个简单的bundle exec rake db:seed。但是,在运行它时,我得到以下输出:

********-C02MGBVJFD57:myapp ***********$ bundle exec rake db:seed Your Gemfile lists the gem factory_girl_rails (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later. rake aborted! NameError: uninitialized constant Faker /Users/**********/workspace/myapp/db/seeds.rb:16:in `block in ' /Users/**********/workspace/myapp/db/seeds.rb:15:in `times' /Users/**********/workspace/myapp/db/seeds.rb:15:in `' /Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed' /Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed' /Users/**********/.rvm/gems/ruby-2.1.2@myapp/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:173:in `block (2 levels) in ' Tasks: TOP => db:seed (See full trace by running task with --trace) 

这是我的seeds.rb文件:

 User.create!( name: "Example User", email: "example@railstutorial.org", password: "foobar", password_confirmation: "foobar", admin: true ) 99.times do |n| name = Faker::Name.name email = "example-#{n+1}@railstutorial.org" password = "password" User.create!( name: name, email: email, password: password, password_confirmation: password ) end 

第16行是:

 name = Faker::Name.name 

任何想法为什么我收到此错误? 谢谢。

刚遇到类似的问题 – 我正在跑步

 rails g model model_name 

并得到错误:

 uninitialized constant Faker (NameError) 

问题是由于事实,我有gem添加到test组。

将它放入development test组解决了这个问题:

 group :development, :test do # ... gem 'faker' # ... end 

我在编写rspec时遇到了同样的问题,在spec文件中添加require 'faker'解决了它。