25

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 <top (required)>'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `times'
/Users/**********/workspace/myapp/db/seeds.rb:15:in `<top (required)>'
/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 <top (required)>'
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

このエラーが発生する理由はありますか? ありがとうございました。

4

2 に答える 2

49

同様の問題に直面しました-実行していました

rails g model model_name

エラーが発生します:

uninitialized constant Faker (NameError)

問題は、testグループに宝石を追加したという事実によるものでした。

それを配置してグループ化するdevelopment test、問題が解決しました:

group :development, :test do
  # ...
  gem 'faker'
  # ...
end
于 2015-03-12T07:18:39.027 に答える
5

rspec の作成中に同じ問題に直面し、require 'faker'spec ファイルを追加して解決しました。

于 2016-08-09T01:56:51.050 に答える