2014-12-12 11 views
11

Tôi cố gắng để chạy một đơn giản bó exec rake db: hạt giống cho cơ sở dữ liệu của tôi trong Rails 4. Tuy nhiên, khi chạy nó, tôi nhận được kết quả như sau:NameError: Uninitialized Faker không đổi; Ruby on Rails

********-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/[email protected]/gems/railties-4.1.4/lib/rails/engine.rb:543:in `load_seed' 
/Users/**********/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:184:in `load_seed' 
/Users/**********/.rvm/gems/[email protected]/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) 

Đây là seeds.rb của tôi file:

User.create!(
    name:     "Example User", 
    email:     "[email protected]", 
    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 

Dòng 16 là:

name = Faker::Name.name 

Bất cứ ý tưởng tại sao tôi nhận được lỗi này? Cảm ơn bạn.

+0

Bạn có đá quý không? – ptd

+0

Tôi có đá quý faker dưới gemfile của tôi trong nhóm: test do – user1072337

+3

Bạn có đang chạy 'rake db: seed' trong môi trường thử nghiệm không? Giả sử bạn đang chạy nó trong phát triển, bạn cần phải thêm nó vào nhóm phát triển của bạn. – ptd

Trả lời

26

Chỉ phải đối mặt với vấn đề tương tự - Tôi đã chạy

rails g model model_name 

và nhận được lỗi:

uninitialized constant Faker (NameError) 

Vấn đề là do thực tế, rằng tôi đã có viên ngọc thêm vào test nhóm.

Đặt nó vào developmenttest nhóm giải quyết vấn đề:

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

tôi phải đối mặt với vấn đề tương tự khi viết rspec và thêm require 'faker' trong file spec giải quyết nó.

+0

trong tệp kiểm tra riêng lẻ hoặc trong tệp rails_helper.rb? – BKSpurgeon

Các vấn đề liên quan