我想用 Faker 播种数据库,问题是我在执行以下操作时遇到错误:
rake db:reset
我收到这条消息:
rake aborted!
I18n::MissingTranslationData: translation missing: en.faker.name.name
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:311:in `handle_exception'
/Library/Ruby/Gems/2.0.0/gems/i18n-0.7.0/lib/i18n.rb:161:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:128:in `rescue in translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:120:in `translate'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:86:in `fetch'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker.rb:99:in `parse'
/Library/Ruby/Gems/2.0.0/gems/faker-1.4.3/lib/faker/name.rb:8:in `name'
/Users/hbendev/code/wikitec/db/seeds.rb:6:in `block in <top (required)>'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `times'
/Users/hbendev/code/wikitec/db/seeds.rb:4:in `<top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Library/Ruby/Gems/2.0.0/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
我不知道为什么会出现这个错误,因为我之前对 Faker 没有任何问题,我只是想重置数据库以更新种子。
我用谷歌搜索,但找不到任何可以解决问题的相关内容。
我试图添加:
I18n.reload!
在require 'faker'
我的seeds.rb 文件之后,但没有运气。
看起来问题出在 Faker 本身,因为数据库正在正确创建,当我执行 a 时rake db:drop db:create db:migrate
它可以工作,直到那里,但是当我尝试使用 Faker 为数据库播种rake db:seed
orrake db:reset
时,我得到了错误。
我能做些什么?提前致谢。
更新-我包括了 seed.rb 和 en.yml 文件
种子.rb:
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
user.save!
end
users = User.all
# Create Wikis
25.times do
Wiki.create!(
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph,
:private => false,
user: users.sample
)
end
# Create Admin account
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld',
role: 'admin'
)
admin.skip_confirmation!
admin.save!
# Create Premium account
premium = User.new(
name: 'Premium User',
email: 'premium@example.com',
password: 'helloworld',
role: 'premium'
)
premium.skip_confirmation!
premium.save!
# Create Standard account
standard = User.new(
name: 'Standard User',
email: 'standard@example.com',
password: 'helloworld',
role: 'standard'
)
standard.skip_confirmation!
standard.save!
puts "Seed finished"
puts "#{Wiki.count} wikis created"
puts "#{User.count} users created"
en.yml:
en:
hello: "Hello world"